aguidrevitch / jquery-file-upload-middleware

jQuery-File-Upload Express.js middleware
294 stars 120 forks source link

Moving uploaded files out of the public directory #54

Closed arosca closed 6 years ago

arosca commented 10 years ago

I managed to setup the middleware with express.js 4.0 but am having difficulties moving uploaded files out of the public directory.

Here's my upload script:

    var upload = require('jquery-file-upload-middleware');
    upload.configure({
        imageVersions: {
            thumbs: {
                width: 80,
                height: 80
            },
            prev: {
                width: 1280,
                height: 1024
            }
        }
    });

    app.use('/admin/upload', function (req, res, next) {
        // imageVersions are taken from upload.configure()
        upload.fileHandler({
            uploadDir: function () {
                return __dirname + '/public/uploads/' + req.session.eventID;
            }
        })(req, res, next);

    });

Uploading a Chicken.jpg file i get the following structure:

    /public/uploads/  -> public uploads folder
        534a8d502e889f8d6bf9cc07/  -> upload session folder
            prev/  -> resized version
                Chicken.jpg
            thumbs/    -> another resized version
                Chicken.jpg
            Chicken.jpg   -> original file

I only want to move the "original file". Can anyone please advise?

Thank you!

Dudemullet commented 10 years ago

You can add an end eventListener to the upload middleware, like so:

upload.on("end", function(fileinfo){
    // do stuff here
})

Look at the More sophisticated example - Events section in the README for all of the events available as well as what information does fileinfo include

arosca commented 10 years ago

I checked the Moving uploaded files out of uploadDir section.

They use a req.filemanager.move method, shouldn't i use that? I am not sure on the syntax and how to structure the code.

lesliepound commented 10 years ago

I use

upload.on("end", function(fileinfo){

    var filemanager = upload.fileManager({
        targetDir: "./../apps/" + app.get('owner') + "/images",
        targetUrl: "/apps/" + fileInfo.sessionID + "/images"
    });

filemanager.move(fileInfo.name, "", function(err, result) {
    console.dir(result);

})

owner is set in the app.use and the sessionID is set in the template.