Skip to content
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

[FeatureRequest]: Warn users when uploading duplicate file names to the media manager #3124

Closed
buuug7 opened this issue Sep 20, 2017 · 32 comments

Comments

@buuug7
Copy link
Contributor

buuug7 commented Sep 20, 2017

description

if upload two same name files into media manager, the files uploaded later will auto overwrite the previous file without prompt. what did i do for prevent auto overwrite? i have many files in one directory,about 5000, when i upload new files into media manager, i must check the file name first, but this become more difficulty when the numbers of files getting more and more .

did there have any good solutions?

@buuug7 buuug7 changed the title if you upload two same name files into media manager,the files uploaded later will overwrite the previous file if upload two same name files into media manager,the files uploaded later will auto overwrite the previous file without prompt Sep 20, 2017
@buuug7 buuug7 changed the title if upload two same name files into media manager,the files uploaded later will auto overwrite the previous file without prompt if upload two same name files into media manager under one directory,the files uploaded later will auto overwrite the previous file without prompt Sep 20, 2017
@kriskornel
Copy link

@buuug7 Just Create a folder then upload the same file into that folder

@buuug7
Copy link
Contributor Author

buuug7 commented Sep 20, 2017

@korneliuskristianr if you edito a article and add images with richeditor with
image
it will auto upload image into uploaded-files directory, when you add a second article , also with a same name image, it will overwrite the previours images. when you see your article, first article and second article have a sample image. but people need it different.

want i need do?

@buuug7
Copy link
Contributor Author

buuug7 commented Sep 20, 2017

if uploaded files generate a random string name , it will prevent this problem. for same image ,user can manager manually, as below, just delete the same images.
github-issue

@tgz-cz
Copy link

tgz-cz commented Sep 20, 2017

I have same problem with the media manager.

I override important file (that was uploaded in past) with newer file with same filename.
None prompt or error or warning was displayed.

I have few questions:

  1. Is there any configuration for change this behaviour to raise error in this case or auto-rename newly uploaded file?

  2. Or is it possible change this behaviour via events/extensions/plugins of MediaManager?

  3. Another way is using my own model with related "File Attachments" ('System\Models\File') instead of MediaManager. There is not problem with overriding existing files, but if user downloads the file then original filename is lost and the filename is 558ab0f0aa231581256800.xls instead.
    It is possible download this file with original filename?

Thank you for answers

@buuug7
Copy link
Contributor Author

buuug7 commented Sep 20, 2017

@tgz-cz i have no best solutions, it seems only by modifying the source code of modules/cms/widgets/MediaManager.php line 1098 ,as follows

    protected function checkUploadPostback()
    {
        $fileName = null;
        $quickMode = false;

        if (
            (!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) &&
            (!$quickMode = post('X_OCTOBER_MEDIA_MANAGER_QUICK_UPLOAD'))
        ) {
            return;
        }

        try {
            if (!Input::hasFile('file_data')) {
                throw new ApplicationException('File missing from request');
            }

            $uploadedFile = Input::file('file_data');
            // remove this line
            $fileName = $uploadedFile->getClientOriginalName();
            // add this line 
            $fileName = str_random(40);

@LukeTowers
Copy link
Contributor

@buuug7 feel free to submit a PR to add a warning popup if the file is going to get overwritten.

@LukeTowers LukeTowers changed the title if upload two same name files into media manager under one directory,the files uploaded later will auto overwrite the previous file without prompt [FeatureRequest]: Warn users when uploading duplicate file names to the media manager Sep 20, 2017
@buuug7
Copy link
Contributor Author

buuug7 commented Sep 21, 2017

@LukeTowers how to determin the two files the same ?

  • just for filepath and filename
  • check the two files MD5
    the MediaLibarary have a method exists, it just detemines a file with the specified path exists in the library.

It not necessary to give the prompt when two files have a same filename, if upload file have the same filename with already updated files. just auto rename the filename. it was simple. that is also work when use richeditor upload file.

source file modules/cms/widgets/MediaManager.php checkUploadPostback() method, add following code.

only throw exception

if(MediaLibrary::instance()->exists($filePath)){
    throw new ApplicationException(Lang::get('cms::lang.media.file_exists'));
}

rename file name

if(MediaLibrary::instance()->exists($filePath)){
    $fileName = File::name($uploadedFile->getClientOriginalName()) . '-' . str_random(40) . '.' . $extension;
    $filePath = $path . '/' . $fileName;
}

which better?

@LukeTowers
Copy link
Contributor

I think the exception is better for now, until we get around to providing a popup that would let the user choose to rename the file on upload.

@buuug7
Copy link
Contributor Author

buuug7 commented Sep 22, 2017

@LukeTowers Ok

@LukeTowers
Copy link
Contributor

Duplicate of #1117, closing that one and keeping this one open.

@MaxNozhkin
Copy link

@daftspunk can you add some event before file upload or when filename is generating?

@LukeTowers
Copy link
Contributor

@MaxNozhkin there's the after upload event: https://octobercms.com/docs/api/media/file/upload. You could make a PR to propose a beforeUpload event if you'd like

@pavelmgn
Copy link
Contributor

pavelmgn commented Sep 9, 2019

I found a solution to rename and return the renamed file to the richeditor, without change source code MediaManager

public function boot()
	{
		Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
			$originalName = $uploadedFile->getClientOriginalName();
			$user = BackendAuth::getUser();
			$sanitizedFileName = Carbon::now()->format('dmYHis') . $user->login . '.' . $uploadedFile->getClientOriginalExtension();
			$filePathChunks = explode(DIRECTORY_SEPARATOR, $filePath);
			$filePathChunks[(count($filePathChunks) - 1)] = $sanitizedFileName;
			$newPath = implode(DIRECTORY_SEPARATOR, $filePathChunks);
			$isRename = true;
			MediaLibrary::instance()->moveFile($filePath, $newPath, $isRename);
			header('Content-Type: application/json');
			echo json_encode(['result' => 'success', 'link' => MediaLibrary::url($newPath)]);
			exit();
		});
	}

@LukeTowers
Copy link
Contributor

@pavelmgn submit a PR to pass the $filePath argument as a reference instead,

header('Content-Type: application/json');
echo json_encode(['result' => 'success', 'link' => MediaLibrary::url($newPath)]);
exit();

is a pretty hacky solution that is unnecessarily brittle. I don't think changing the event argument for this case to pass by reference instead is going to cause any breaking changes so submit a PR and I'll merge it in.

@pavelmgn
Copy link
Contributor

@LukeTowers #4612
Sorry, if I did something wrong, the first time I did PR. Did that mean?

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.

@bennothommo
Copy link
Contributor

bennothommo commented Oct 14, 2019

#4616 makes $filePath a referenced variable, allowing the filePath to be changed via the media.file.upload event and providing a mechanism to prevent duplicate images and files. This will be released with build 460.

@setianke
Copy link

@LukeTowers agreed, imo an option in the Settings enabling the automatic date in filename on/off or show warning with cancel/replace/keep both would be a good solution (just my 2cents)

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@daftspunk
Copy link
Member

This is a good pickup. I have recorded this to be addressed in the next iteration of media manager. Thanks @buuug7!

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

No branches or pull requests

9 participants