Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a file object with a path in NodeJS?

I want to know if it is possible to create a file object (name, size, data, ...) in NodeJS with the path of existing file ? I know that it is possible in client side but I see nothing for NodeJS.

In others words, I want the same function works in NodeJS :

function srcToFile(src, fileName, mimeType){
    return (fetch(src)
        .then(function(res){return res.arrayBuffer();})
        .then(function(buf){return new File([buf], fileName, {type:mimeType});})
    );
}

srcToFile('/images/logo.png', 'logo.png', 'image/png')
.then(function(file){
    console.log(file);
});

And ouput will be like :

File {name: "logo.png", lastModified: 1491465000541, lastModifiedDate: Thu Apr 06 2017 09:50:00 GMT+0200 (Paris, Madrid (heure d’été)), webkitRelativePath: "", size: 49029, type:"image/png"…}
like image 549
Pouchopa Avatar asked Apr 05 '17 12:04

Pouchopa


People also ask

How do I declare an object in NodeJS?

Objects can be created with new Object constructor. Attributes are then dynamically added using dot operator. let person = new Object(); person. firstName = "John"; person.


2 Answers

For those that are looking for a solution to this problem, I created an npm package to make it easier to retrieve files using Node's file system and convert them to JS File objects:

https://www.npmjs.com/package/get-file-object-from-local-path

This solves the lack of interoperability between Node's fs file system (which the browser doesn't have access to), and the browser's File object type, which Node cannot create.

3 steps are required:

  1. Get the file data in the Node instance and construct a LocalFileData object from it
  2. Send the created LocalFileData object to the client
  3. Convert the LocalFileData object to a File object in the browser.
// Within node.js
const fileData = new LocalFileData('path/to/file.txt')

// Within browser code
const file = constructFileFromLocalFileData(fileData)
like image 93
abr Avatar answered Sep 18 '22 09:09

abr


So, I search with File Systems and others possibilities and nothing. I decide to create my own File object with JSON.

  var imagePath = path.join('/images/logo.png', 'logo.png');

  if (fs.statSync(imagePath)) {
     var bitmap = fs.readFileSync(imagePath);
     var bufferImage = new Buffer(bitmap);

     Magic = mmm.Magic;
     var magic = new Magic(mmm.MAGIC_MIME_TYPE);
     magic.detectFile(imagePath, function(err, result) {
          if (err) throw err;
          datas = [{"buffer": bufferImage, "mimetype": result, "originalname": path.basename(imagePath)}];
          var JsonDatas= JSON.parse(JSON.stringify(datas));
          log.notice(JsonDatas);
     });
 }

The output :

{ 
  buffer:
  { 
    type: 'Buffer',
    data:
    [ 
      255,
      216,
      255
      ... 24908 more items,
      [length]: 25008 
    ] 
  },
  mimetype: 'image/png',
  originalname: 'logo.png' 
}

I think is probably not the better solution, but it give me what I want. If you have a better solution, you are welcome.

like image 20
Pouchopa Avatar answered Sep 18 '22 09:09

Pouchopa