Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file size using url, in nodejs

I have some images in url(s). I can get file properties and properties include width and height of image as well. I want to get the size in bytes.

I am trying to get size using fs module as shown below, but it is not working with url, though it works with file path in local folder.

var stats = fs.statSync(url);
var fileSizeInBytes = stats["size"]
like image 594
Umair Jameel Avatar asked Apr 03 '18 14:04

Umair Jameel


People also ask

How do I check the size of a Nodejs file?

To get the size of a file in Node. js, you can use the stat() method provided by the built-in fs module. This method works asynchronously and lists the statistics of a file at the given path. The size of the file is returned in bytes.

How do you find buffer size?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

What is FS statSync?

The fs. statSync() method is used to synchronously return information about the given file path.

How do I delete a FS file?

In Node. js, you can use the fs. unlink() method provided by the built-in fs module to delete a file from the local file system.


2 Answers

You have to use request, or http. You can get the file size by sending a HEAD request and inspect content-length field (it will not work on every server):


With curl:

curl -I "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js"

You get the response:

HTTP/1.1 200 OK
Date: Tue, 03 Apr 2018 14:30:16 GMT
Content-Type: application/javascript; charset=utf-8
Content-Length: 9068
Connection: keep-alive
Last-Modified: Wed, 28 Feb 2018 04:16:30 GMT
ETag: "5a962d1e-236c"
Expires: Sun, 24 Mar 2019 14:30:16 GMT
Cache-Control: public, max-age=30672000
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Accept-Ranges: bytes
Strict-Transport-Security: max-age=15780000; includeSubDomains
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 405c3b6e1911a8db-CDG

With request module :

var request = require("request");

request({
    url: "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js",
    method: "HEAD"
}, function(err, response, body) {
    console.log(response.headers);
    process.exit(0);
});

You get the response:

{
  date: 'Tue, 03 Apr 2018 14:29:32 GMT',
  'content-type': 'application/javascript; charset=utf-8',
  'content-length': '9068',
  connection: 'close',
  'last-modified': 'Wed, 28 Feb 2018 04:16:30 GMT',
  etag: '"5a962d1e-236c"',
  expires: 'Sun, 24 Mar 2019 14:29:32 GMT',
  'cache-control': 'public, max-age=30672000',
  'access-control-allow-origin': '*',
  'cf-cache-status': 'HIT',
  'accept-ranges': 'bytes',
  'strict-transport-security': 'max-age=15780000; includeSubDomains',
  'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  server: 'cloudflare',
  'cf-ray': '405c3a5cba7a68ba-CDG'
}
like image 161
Daphoque Avatar answered Oct 02 '22 11:10

Daphoque


file_size_url



Get file size from URL without downloading it. 0 dependencies.

Returns Promise with 'B', 'KB', 'MB', 'GB', 'TB' on success.

npm i file_size_url


import file_size_url from 'file_size_url';

file_size_url("https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/001.mp3")
    .then(console.log) // 968.27 KB
    .catch(console.error);

OR


import file_size_url from 'file_size_url';

let size = await file_size_url("https://serverjyy10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/0001.mp3")
    .catch((error) => console.log(error))

console.log(size) // 968.27 KB


Example for lop


let array = [

    'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/001.mp3',
    'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/002.mp3',
    'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/003.mp3',
    'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/055.mp3',
    'https://server10.mp3quran.net/bader/Rewayat-Hafs-A-n-Assem/110.mp3',

]

for (let index = 0; index < array.length; index++) {

    try {

        let fies_size = await file_size_url(array[index])

        if (fies_size.toString().split('.')[0] <= 95 && fies_size.toString().split(' ')[1] === 'MB') {

            console.log(fies_size + ' || <= 95 MB');

        }

        else if (fies_size.toString().split('.')[0] > 95 && fies_size.toString().split(' ')[1] === 'MB') {

            console.log(fies_size + ' || > 95 MB');

        }

        else if (fies_size.toString().split(' ')[1] === 'KB') {

            console.log(fies_size + ' || KB');

        }


    } catch (error) {

        console.log(error);

    }

}


/* output 
968.27 KB || KB
170.58 MB || > 95 MB
95.77 MB || <= 95 MB
12.21 MB || <= 95 MB
711.92 KB || KB
*/


like image 21
Ryan Almalki Avatar answered Oct 02 '22 11:10

Ryan Almalki