Skip to content

Fix #23025: Avoid unnecessary copying files #24570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

crazytonyli
Copy link
Contributor

Description

In the process of uploading an image from the Photos app, there are two places that creates file using mediaFileManager, with the same filename and at the same directory.

  1. Copying the selected image file, which is in a temp directory, to the app's "local media directory". That's done by the code that's moved in this commit.
  2. Creating a new image based on the selected image file, for the purpose of removing some exif data.

Since these two operations creates the same file at the same directory, the second copy creates a file with "-1" suffix, which is uploaded to the site's media library. That's the root cause of why there is a "-1" in the title of uploaded images.

Testing instructions

Verify the issue is fixed, on WP.com sites and self-hosted sites.

In the process of uploading an image from the Photos app, there are two
places that creates file using `mediaFileManager`, with the same
filename and at the same directory.
1. Copying the selected image file, which is in a temp directory, to the
   app's "local media directory". That's done by the code that's moved
   in this commit.
2. Creating a new image based on the selected image file, for the
   purpose of removing some exif data.

Since these two operations creates the same file at the same directory,
the second copy creates a file with "-1" suffix, which is uploaded to
the site's media library. That's the root cause of why there is a "-1"
in the title of uploaded images.
@crazytonyli crazytonyli added this to the 26.0 milestone May 30, 2025
@crazytonyli crazytonyli requested review from jkmassel and kean May 30, 2025 10:10
@dangermattic
Copy link
Collaborator

1 Warning
⚠️ This PR is assigned to the milestone 26.0. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger

Copy link

@wpmobilebot
Copy link
Contributor

App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number27812
VersionPR #24570
Bundle IDorg.wordpress.alpha
Commit23bad6d
Installation URL2majdpeeoo9u8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot
Copy link
Contributor

App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number27812
VersionPR #24570
Bundle IDcom.jetpack.alpha
Commit23bad6d
Installation URL4cl5o4hp6qaeg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

Copy link
Contributor

@kean kean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on photos and GIFs and it worked as expected.

Note: when you upload the same image the second time, it adds -1 (presumable server-side?).

Left one optional comment.

@@ -67,15 +70,18 @@ final class ItemProviderMediaExporter: MediaExporter {

// Retaining `self` on purpose.
do {
let copyURL = try self.mediaFileManager.makeLocalMediaURL(withFilename: url.lastPathComponent, fileExtension: url.pathExtension)
try FileManager.default.copyItem(at: url, to: copyURL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unsafe to remove this code because of how loadFileRepresentation works:

This method writes a copy of the file’s data to a temporary file, which the system deletes when the completion handler returns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I think it's best to keep the copying code. I have created another solution in #24595.

@crazytonyli
Copy link
Contributor Author

Note: when you upload the same image the second time, it adds -1 (presumable server-side?).

I noticed that too. It's added by the app. The app copies the image files to a specific "local Media" directory (mediaFileManager.makeLocalMediaURL(..)). The file for the second upload is automatically added a -1 prefix because the file used in the first upload is still in the directory. I didn't look further because it's rare that users would want to upload the same image twice.

However, keeping the uploaded files in the local Media directory means that, in theory, the storage the app uses will continue to increase as users upload more files. That's probably something worth looking into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants