Plupload, renames files strangely

292 Views Asked by At

On a platform on which I take care of maintenance, a strange phenomenon has appeared.

On the site, we use plupload to add images and files. Since a few days, for a dozen users (in Austria, Switzerland and Germany among others), during the upload, the files are renamed.

If I send "my_image.png", it saves it in the repository under this name or it adds (1) if another file already exists under this name. Currently, it systematically renames files with --1 or --2 [...] (for example : my_image--1.png). This would not be an issue if it took this change into account when registering the file name in a database, which is not the case.

Despite the help of my colleagues, and the research, there is no explanation for this situation. It is ultra-partial and has already taken place at the end of 2016 without having been detected. Has anyone ever encountered this problem?

I give you our code of our plupload.

For additional information, our users use Internet Explorer (last version) and on our side, no bug is raised, our attempts to reproduce the bug is in vain.

Thanks for your attention

function addUpload(form_,type_,container_,button_,multiple_,ext_,params) {
    var extended_parameters = new Array();
    type_ = 'file_name_'+type_+'[]';
    return addUploadApply(form_,type_,container_,button_,multiple_,ext_,params, extended_parameters);
}

//La principale différence entre addUpload et addUploadV2 est que pour la seconde, le name fourni pour le champ (variable type_ = input_name) n'est pas réécrit par la fonction, il est utilisé tel quel ce qui est bien plus pratique pour le dévelopepment
function addUploadV2(form_id, input_name, container_id, upload_button_id, multiple_, ext_, http_get_params, extended_parameters) {
    return addUploadApply(form_id, input_name, container_id, upload_button_id, multiple_, ext_, http_get_params, extended_parameters);
}

//Cette fonction a été redéfinie dans le cadre de MultiALL
//On y ajoute un tableau de parameters afin de gérer d'éventuels paramètres supplémentaires, ce qui est compliqué à faire à l'heure actuelle
//Ca nous servira plus tard
function addUploadApply(form_,type_,container_,button_,multiple_,ext_,params, extended_parameters){ nb_upload_fields++;

    var nb_fichiers = 0;
    var total_files_size = 0;

    var file_upload = '/backoffice/upload.php?'+params;

    var uploader = new plupload.Uploader({
        multi_selection: false,
        form : form_,
        runtimes : 'gears,html5,flash,silverlight,browserplus,html4',
        browse_button : button_,
        container : container_,
        //max_file_size : '2mb',
        max_queue_size : '62914560', // 60 Mo
        url : file_upload,
        flash_swf_url : '/plupload/plupload.flash.swf',
        silverlight_xap_url : '/plupload/plupload.silverlight.xap',
        filters : [
            {title : "Files", extensions : ext_}
        ]
    });

    uploader.init();


    uploader.bind('FilesAdded', function(up, files) {

        /*$('#next_btn').hide();*/
        if(!multiple_ && nb_fichiers > 0){
            $('#'+container_+' .inputlist, #'+container_+' .filelist').html("");
            nb_fichiers = 0;
        }

        $.each(files, function(i, file) {
            total_files_size += file.size;
        });

        if(total_files_size < uploader.settings.max_queue_size){

            $.each(files, function(i, file) {

                nb_fichiers++;
                c_ = '<div class="col-md-4" id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>';

                if(multiple_){ $('#'+container_+' .filelist').append(c_); }
                else{ $('#'+container_+' .filelist').html(c_); }

            });

            nb_upload_uploading++;
            up.refresh(); // Reposition Flash/Silverlight
            uploader.start();

        }else{
            $.each(files, function(i, file) {
                total_files_size -= file.size;
                up.removeFile(file);
            });
            if(!$('#'+container_+' .filelist div').length) $('#'+container_+' .filelist').append('<div></div>');
            $('#'+container_+' .filelist div').prepend('<b>ERROR</b> ').css('color', '#ff0000');
            alert('Error : 11 The selected file is too big.');
            //$('#plupload_error').modal();
        }

    });

    uploader.bind('UploadProgress', function(up, file) {
        $('#'+uploader.settings.form+' #' + file.id + " b").html(file.percent + "%");
    });

    uploader.bind('Error', function(up, err) {

            $('#'+container_+' .filelist div').prepend('<b>ERROR</b> ').css('color', '#ff0000');
            alert('Error : '+obj.error.code+' '+obj.error.message);

        /*$('#'+uploader.settings.form+' #filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );*/

        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('FileUploaded', function(up, file, info) {
        $('#'+container_+' .inputlist ' + file.id + " b").html("100%");
        var obj = JSON.parse(info.response);

        // Détection d'une erreur dans le PHP
        if (obj.error !== undef) {
            $('#'+container_+' .filelist div').prepend('<b>ERROR</b> ').css('color', '#ff0000');
            alert('Error : '+obj.error.code+' '+obj.error.message);
            return false;
        }

        var c_ = '<input type="hidden" name="'+type_+'" value="' + obj.result.cleanFileName + '" class="uploadedFile ' + file.id + '" />';
        if(multiple_){ $('#'+container_+' .inputlist').append(c_); }
        else{ 
      //$('#'+container_+' .inputlist').html("");
      if ($('#'+container_+' .inputlist .uploadedFile').length && $('#'+container_+' .inputlist .uploadedFile').attr('name') != '' ) $('#'+container_+' .inputlist .uploadedFile').val(obj.result.cleanFileName); 
      else $('#'+container_+' .inputlist').html(c_);
    }
    if ($('#'+container_+' .fileRemoveButton').length) $('#'+container_+' .fileRemoveButton').show(); 
    });

    uploader.bind('UploadComplete', function(up, file) {
        nb_upload_completed++;
        if(check_upload()){ $('#next_btn').show(); }
    });

}
2

There are 2 best solutions below

5
On

I do not know how to properly fix it, but what I do and advise you to do is to force the files to have unique names, to ensure that no conflicts will occur.

You can do that by appending a pseudo-random hash to each files, thus making their names random (but still keeping the original name). This will prevent collision and you will have full control over and knowledge of the final file.

0
On

The problem is resolved and didn't come from the plugin. Another developer has modified (when I was sick) the code to duplicate content. It changed without regard to what's around and since then, it was no longer working properly.

Thank you for your attention and help.