v1/filesystem/file/exists and .../folder/exists - help needed

Started by axel.hennig, October 26, 2022, 09:50:10 PM

Previous topic - Next topic

axel.hennig

Hello everybody,

I would likt to use the "v1/filesystem/file/exists" endpoint, but I don't get it to work. When running the follwing code

IMWS.get('v1/filesystem/file/exists',{
    filename: 'D:\\z_tmp\\test.txt'
}).then(function (response) {
    console.log('responseFile: ' + JSON.stringify(response,null,2));
},function(error) {
    console.log('errorFile: ' + JSON.stringify(error,null,2));
});

IMWS.get('v1/filesystem/folder/exists',{
    path: 'D:\\z_tmp'
}).then(function (response) {
    console.log('responseFolder: ' + JSON.stringify(response,null,2));
},function(error) {
    console.log('errorFolder: ' + JSON.stringify(error,null,2));
});

the result in the console (DevTools in Chrome) is:

errorFile: {
  "readyState": 4,
  "responseText": "\"exists\":true",
  "status": 200,
  "statusText": "OK"
}
responseFolder: ""

I've also used the "v1/filesystem/folder/exists" because my expectation was that both endpoints should work similar.

My questions / what I don't understand:
  • Why does the "file/exits" endpoint seems to return the result in the error-variable and not in the response-variable?
  • Why does the "folder/exists" endpoint return an empty string?
  • I was expecting something which could be easily used in an if-else-statement. Is this somehow possible?

Mario

The file/exists endpoint is buggy. It does not return a valid JSON object but a plain string, which is what causes the problem in the browser / JS.

I have changed it to return 404 (not found) when the file exists for the next version, so it works like folder/exists then.

In the meantime, just catch the error thrown by JavaScript and check the response for the "exists":true text

let fileExists = false;
try {
    response = await IMWS.get('v1/filesystem/file/exists', {
        filename: 'C:\\images\\beach.jpg'
    });
}
catch (e) {
    fileExists = e.responseText === '"exists":true';
}
console.log(`File exists: ${fileExists}.`);
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook