Meteor-files: Filename in FS

Created on 4 Oct 2016  路  20Comments  路  Source: veliovgroup/Meteor-Files

I save video files with:

var uploadData = {
        file: data,
        fileName: 'ID_'+_id+'_TRY_'+this.state.count+'.webm',
        isBase64: true,
        streams: 'dynamic',
        chunkSize: 'dynamic',
        meta: {_id:_id}
      }
      var uploadInstance = Videos.insert(uploadData, false);

and get returned an obj with:

fileId:"g9E3nXaYKDCWo5egN"
fileName:"ID_84f2eb0a163ebb2f81a10b01_TRY_3.webm"

In my fs the file is saved as g9E3nXaYKDCWo5egN.webm
I want it to be saved as ID_84f2eb0a163ebb2f81a10b01_TRY_3.webm

How do I do that?
Thank you very much.
Finn

question

All 20 comments

Hi @FinnFrotscher

You're looking for namingFunction passed to Constructor. But I'm afraid it's not possible to implement what you want, moreover I wouldn't recommend to change default behaviour.

The fileName you've used - sets filename which returned by server, for example when file is being downloaded.

Same question is widely discussed in #100 and #117 , please read both threads.

I will.
BTW I have a Bounty up on bountify thats about to expire. I solved the issue already, cant redeem the funds and you helped me A LOT!
If you want you can claim it. A working answer is in the comments.

@FinnFrotscher thank you for support. Done.
I'm glad you found my help useful. Are you okay to be added to the supporters section?

Yes of course.

@FinnFrotscher will you close this thread, or there is some question left?

I have yet to read through the threads. It will be done by tomorrow morning and then I can decide.

Hey @FinnFrotscher , any news ?

Yes a question.
When I have the config.namingFunction setup on this.Videos = new FilesCollection, how do i pass params to it?

Params and context is passed upon moment when lib needs to get a name for a file.
On upload and FS write.
Just drop there console.log(this, arguments); so see what you can work with

Works on localhost.
We push to server sometime this week probably. Then I will close.

Solution:
lib/collection.js

this.Videos = new FilesCollection({
  collectionName: 'Videos',
  allowClientCode: false,
  storagePath: '/data/imde',
  namingFunction: (args) => {
    console.log('\n namingFunction says: ');
    console.log(args.name);
    return args.name
  }
});

client.js

      var uploadData = {
        fileName: 'ID_'+_id+'_TRY_'+this.state.count+'.webm',
        ...
       }

      var uploadInstance = Videos.insert(uploadData, false);

@FinnFrotscher
I should warn you what this method allows to affect server side in naming files on FS from untrusted client.

any precautions you suggest?

Replicate same logic you use in fileName inside namingFunction without passing there untrusted data which coming from _Client_

How do I pass params to the namingFunction from the client?
when I use the args in namingFunction: (args) => {}, where is the benefit of doing to the logic on the server?

No need to pass, as I said before - check function's context and arguments

Where is the improvement, when I pass two variables and combine then on the server (args)=>{return ID_'+args._id+'_TRY_'+args.count} versus fileName: 'ID_'+_id+'_TRY_'+this.state.count on the client?

Everything except meta on fileObj (basically record from mongo) is trusted source.
Like _id in your example. Idk where count comes from, so I can not say is it trusted enough

Hi @FinnFrotscher ,
Let me know what you final solution, and how it's working on your end.

It works fine :)
We went with my original solution. fileName: 'ID_'+_id+'_TRY_'+this.state.count
Both ID and count are custom vars. But on the server we do make sure the string is of a certain length and such.

@FinnFrotscher nice to hear that.

Was this page helpful?
0 / 5 - 0 ratings