Skip to content

Commit 149bf6e

Browse files
dcalhounclaude
andauthored
fix: rename cached media files to include extension when missing (#22753)
Path resolution can drop the file extension when the source filename contains spaces (e.g. a Samsung screenshot named "screenshot_one ui home123.jpg" resolves to a cached path ending in "screenshot_one ui home123."). The existing code already derives the extension from the MIME type and appends it to the filename, but the on-disk file path was left unchanged. This caused downstream upload logic (wordpress-rs / reqwest) to fail MIME type detection from the path, resulting in "not allowed to upload this file type" errors. Rename the cached file on disk to include the correct extension so that the file path stored in the MediaModel is consistent with the filename. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f21fd62 commit 149bf6e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

WordPress/src/main/java/org/wordpress/android/util/FluxCUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ public static MediaModel mediaModelFromLocalUri(@NonNull Context context,
144144
filename += "." + fileExtension;
145145
}
146146

147+
// Path resolution can drop the file extension when the filename contains
148+
// spaces (e.g. "screenshot_one ui home123.jpg" resolves to a path ending
149+
// in "screenshot_one ui home123."). Rename the on-disk file so the path
150+
// includes the correct extension, allowing upload consumers to detect the
151+
// MIME type from the filename.
152+
if (fileExtension != null && !path.endsWith("." + fileExtension)) {
153+
String correctedPath = path;
154+
while (correctedPath.endsWith(".")) {
155+
correctedPath = correctedPath.substring(0, correctedPath.length() - 1);
156+
}
157+
correctedPath = correctedPath + "." + fileExtension;
158+
File renamed = new File(correctedPath);
159+
if (file.renameTo(renamed)) {
160+
path = correctedPath;
161+
} else {
162+
AppLog.w(T.UTILS,
163+
"Failed to rename cached file to include extension: " + path);
164+
}
165+
}
166+
147167
MediaModel media = new MediaModel(
148168
localSiteId,
149169
DateTimeUtils.iso8601UTCFromTimestamp(System.currentTimeMillis() / 1000),

0 commit comments

Comments
 (0)