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

putFileContents() lost support for File objects in 4.x #266

Open
lazka opened this issue Jun 30, 2021 · 8 comments
Open

putFileContents() lost support for File objects in 4.x #266

lazka opened this issue Jun 30, 2021 · 8 comments

Comments

@lazka
Copy link

lazka commented Jun 30, 2021

(Using the web version, v4.6.0)

Passing a File object to putFileContents() results in calculateDataLength() throwing an error because it can't detect the size. Passing contentLength: file.size makes things work again like with 3.x

Could putFileContents() be changed to officially support File objects? It seems simple enough, unless I'm missing something.

@perry-mitchell
Copy link
Owner

perry-mitchell commented Jul 1, 2021

@lazka Hmm, I wasn't aware that anyone had tried to use a file object to upload some data 😅 - But it makes sense to me. You could simply const buff = await file.arrayBuffer() and upload that, but that would require reading the whole file into memory.

We could add a check here for WEB being set correctly so we can check if the object is a file, and then calculate the size correctly. Or we could simply had a node+web friendly File check here. That being said, I don't think we should set the Content-Length at all on web - I'm pretty sure that's disallowed. I can't find a reference for that but I've had experience in setting it and getting warnings in the browser console.

I'd accept a PR for this.

EDIT: It would need a web-specific test as well.

@ashil5834
Copy link

Hey there,
I am using client.putFileContents to write a file,I need to throw an error specifically whenever an error happened in that promise, for example if I say, if the path I have given is wrong it should throw an error like this 'path doesn't exist' and also if the folder not having any permission to write it should throw the error specifically.

@perry-mitchell
Copy link
Owner

@ashil5834 Please add an example of where you think it should fail.

@ashil5834
Copy link

ashil5834 commented Aug 16, 2021

@perry-mitchell here is the example,

const { createClient } = require("webdav");

const client = createClient(
    "https://devl-sync.citushealth.com/account/CHA_AU761ZA9",
    {
        username: "test",
        password: "test",
        
    }
)


async function first_folder() {

    
    const directoryItems = await client.getDirectoryContents("/PN/test1_dir").then(function (contents) {
        contents.forEach((items) => {
            var des = items.filename.split('/')
            var dest = des[des.length - 1]
        
            const str = client.getFileContents(items.filename, { format: "text" }).then((content) => {
           
                const wr = client.putFileContents('/Archive/first_dirtttt/' + dest, JSON.stringify(content)).catch((err)=>{
                    if(err) console.log(err)
                  
                });
                 
            })

        })
        
    })
      
    
}
first_folder()

@perry-mitchell
Copy link
Owner

Ok, so that example (while mixing promises and callbacks) doesn't exactly indicate a potential failure. The promise should fail if there's a permissions issue.

Try using async/await notation a bit more to ensure that you're seeing any errors. I've tidied up your example:

const { createClient } = require("webdav");

const client = createClient(
    "https://devl-sync.citushealth.com/account/CHA_AU761ZA9",
    {
        username: "test",
        password: "test",
        
    }
)


async function first_folder() {
    const directoryItems = await client.getDirectoryContents("/PN/test1_dir");
    for (const item in directoryItems) {
        const des = item.filename.split('/');
        const dest = des[des.length - 1];
        const content = await client.getFileContents(item.filename, { format: "text" });
        await client.putFileContents('/Archive/first_dirtttt/' + dest, JSON.stringify(content));
    }
}

first_folder().catch(err => {
    console.error(err);
});

@ashil5834
Copy link

ashil5834 commented Aug 23, 2021

@perry-mitchell
I have tried with this, the problem is that when I had given a wrong folder name, it is throwing an error like this "Error: Invalid response: 403 Forbidden.......", kind of permission issue, my requirement is to show the error specifically like " 404 or path does not exist", and if there is any permission issue, have to show "no permission to access this folder"......need to understand what is the error, Is there any solution for that.

@hironico
Copy link

@lazka Hmm, I wasn't aware that anyone had tried to use a file object to upload some data 😅 - But it makes sense to me. You could simply const buff = await file.arrayBuffer() and upload that, but that would require reading the whole file into memory.

We could add a check here for WEB being set correctly so we can check if the object is a file, and then calculate the size correctly. Or we could simply had a node+web friendly File check here. That being said, I don't think we should set the Content-Length at all on web - I'm pretty sure that's disallowed. I can't find a reference for that but I've had experience in setting it and getting warnings in the browser console.

I'd accept a PR for this.

EDIT: It would need a web-specific test as well.

Hi,
Indeed, tried to set the Content-Length in PutFileContentsOptions while uising client.putFileContents function.
The upload actually worked but the browser tells that it could not set the unsafe header Content-Length.

@perry-mitchell
Copy link
Owner

perry-mitchell commented Apr 6, 2022

the browser tells that it could not set the unsafe header Content-Length

Then it didn't set the header - therefore it can't be the reason it succeeded.

That's why we have the detection to set it or not: https://github.com/perry-mitchell/webdav-client/blob/master/source/operations/putFileContents.ts#L31-L39

Rhilip added a commit to pt-plugins/PT-Plugin-Plus that referenced this issue May 6, 2024
Rhilip added a commit to pt-plugins/PT-Plugin-Plus that referenced this issue May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants