Node fs get image/video


#1

I’m using electron to bundle my web app as a desktop app and I need to use node’s fs module to interact with the local file system.
I’ve managed to get it working with reading/writing text files but that’s just a simple test and what I actually need it to do is to read images (probably just jpgs) and videos (probably .mov). I would then like to serve these to my angular2 application.
If anyone could help me with some basic examples for this case I would be very grateful

fyi, I was using some thing like this in my main.ts in the server folder to read text files:

import {Meteor} from 'meteor/meteor';
import * as fs from "fs";
 
Meteor.startup(function () {
    Meteor.methods({
      'readTextFile': function(url) {
        fs.readFile(String(url),'utf-8', function read(err, data) {
          if (err) {
              throw err;
          }

          return data;
        });
      }
});

#2

Could someone help here? I thought I was successfully reading a text file but even that isn’t working. I’m just getting undefined returned. I understand that this is because it’s an asynchronous call so I’ve put it in a callback function but it’s still not working.
Could someone provide a basic example? (I’m using angular2 if that changes how the reactivity might be handled…)


#3

Bump. I still haven’t been able to get this working…


#4

Finally got this working. If anyone is interested this is what i’m using:

Server:

  Meteor.methods({

    readTextFile: function(url) {
      console.log('read file from: ' + url);

      var Future = Npm.require('fibers/future');
      var myFuture = new Future();

        fs.readFile(String(url),'utf-8', function read(error, result) {
          if(error){
            myFuture.throw(error);
          }else{
            myFuture.return(result);
          }
        });

        return myFuture.wait();
    },

Client:

  readTextFile() {
    var url = '/path/to/file';

    var result = Meteor.call('readTextFile', url, function (error,res) {
          if (error) {
            console.log(error);
          }
          else {
            console.log(res);
          }
    });
  }

#5

Although I can’t seem to set an angular variable from the meteor callback now! I thought it was because its not inside the angular zone? So i tried wrapping it in NgZone like:

Meteor.call('readTextFile', url, function (error,res) {
    this._ngZone.run(() => {
      if (error) {
        console.log('ERROR:');
        console.log(error);
      }
      else {
        this.text = 'success!';
        console.log(res);
      }
    });
});

But that just gives me the following error:

Exception in delivering result of invoking 'readTextFile': TypeError: Cannot read property 'run' of undefined

Can anyone tell me what im doing wrong?