NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Spaces in filenames don't upload #193

Closed seivad closed 5 years ago

seivad commented 5 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

I've found that choosing files with spaces in their filename do not upload, I receive a:

progress
progress
complete

and that is all. But when uploading files without spaces, it works correctly.

I have since tried doing a replacement on the filename and storing that file as a tmp file and uploading from that, which console.logs out correctly with the updated filename with dashes replacing spaces but it still fails to upload. Results in 2 Progress and 1 Complete logs and that is all. The file is zero bytes when it uploads.

All normal files with underscores or dashes upload correctly.

I've left comments and my testing code below to show you what I've attempted / tried. It seems like a bug on the plugins end though as it shouldn't matter if I choose a file with spaces or not.

let files = e.object.get("results");
    console.log("Files:", files)

    const file = fs.File.fromPath(
        files[0].file.replace(/file:\/\//, "")
    );

    const documents = fs.knownFolders.temp()
    var fullPath = documents.path + '/' + encodeURI(file.name.replace(/%20/g,'-'))
    var tmpFile = fs.File.fromPath(fullPath)
    tmpFile.writeSync(file.readSync())

    // console.log(tmpFile.name)

    const newTmpFile = fs.File.fromPath(
        tmpFile.path
    );

    console.log(newTmpFile)

    // filename = tmpFile.replace(/ /g,"-").replace(/[^a-zA-Z0-9]_/g, '-').replace(/\(|\)/g, "-").replace(/_+/g, '-').toLowerCase()

    var request = {
        url: `${env.API_ROOT}/api/projects/${this.project.id}/files`,
        method: "POST",
        headers: {
            'Authorization': `Bearer ${LS.getItem("access_token")}`,
            // "Content-Type": "application/octet-stream",
            'Content-Type': 'multipart/form-data',
            "File-Name": newTmpFile.name
        },
        description: "{ 'uploading': '" + newTmpFile.name + "' }"
    };

    console.log("FILE:", newTmpFile)

    // var task = this.session.uploadFile("file://" + filepath, request);

    var task = this.session.multipartUpload([{ name: 'file', filename: newTmpFile.path }], request);

    task.on("progress", logEvent);
    task.on("error", logEvent);
    task.on("complete", logEvent);

    function logEvent(e) {
        console.log(e.eventName);
        return
    }
elena-p commented 5 years ago

Hi @seivad,

I have tested the upload with filenames including spaces within the demo app with the demo server, and the files uploaded successfully. Could you check what is the size of the file at the beginning, as fs.File.fromPath gets or creates a file with path and it is possible that new file is created instead of returning the expected one.

elena-p commented 5 years ago

closing due to inactivity