Get online image file size without downloading the file

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: xbytor, DavideBarranca, Paul MR

Post Reply
Limey
Posts: 4
Joined: Tue Jan 30, 2018 7:51 pm

Get online image file size without downloading the file

Post by Limey » Wed Aug 21, 2019 9:42 pm

Is it possible to get the size of an image file from a URL before downloading it? I found the following code online but I read elsewhere that Photoshop/ExtendScript can't use XMLHttpRequest.

Code: Select all

function getFileSize(url)
{
    var fileSize = '';
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false); // false = Synchronous

    http.send(null); // it will stop here until this http request is complete

    // when we are here, we already have a response, b/c we used Synchronous XHR

    if (http.status === 200) {
        fileSize = http.getResponseHeader('content-length');
        console.log('fileSize = ' + fileSize);
    }

    return fileSize;
}
Source: https://bitexperts.com/Question/Detail/ ... ing-a-file

The reason I am trying to do this is to compare the file before downloading to the downloaded file. What is happening is that I am using cURL to download the file and before it finishes downloading Photoshop places it into my document. This leads to a black block of missing data in the image.

Code: Select all

app.system("curl -o ~/Desktop/jpegs/Download_Folder/" + skuNumber + ".jpeg https://mysite.com/image/" + skuNumber + ".fpx?qlt=100&printres=300&fmt=jpeg")
Currently my workaround is to check if the file exists in the download folder and loop until it does. Then sleep 1 sec. then check the file size locally using filePath.length. I then wait 500ms and check again. If they are different values, I wait 500ms and check again until the last file size matched the previous and break out of the loop.

This is all fine except if there is a lag spike and the file size doesn't grow, it breaks out and places an incomplete image file in my document.

So, my thought is, if I can get the size of the original file in bits and check the downloaded file against it, it solves a lot of problems.

Any thoughts?

Thanks,
Paul

Post Reply