feathersjs-ecosystem / feathers-blob

Feathers service for blob storage, like S3.
http://feathersjs.com
MIT License
92 stars 32 forks source link

feathers blobs service save docx with .bin extension #59

Closed HarisHashim closed 5 years ago

HarisHashim commented 5 years ago

Steps to reproduce

"data:application/octet-stream;base64,UEsDBBQABgAIAAAAIQCz8LkduwEAAFwIAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ... (continue lots of stuff)"

Expected behavior

Expected file is saved with docx extension.

Actual behavior

It is saved in upload folder with .bin extension

System configuration

Module versions (especially the part that's not working):

"@feathersjs/feathers": "^3.1.7",
"feathers-blob": "^2.0.1",
"fs-blob-store": "^5.2.1"

NodeJS version: v8.11.2

Operating System: Win 10 Pro

Browser Version: Google Chrome Version 73.0.3627.0 (version does not matter)

Code Structure:

I have uploads.service.js (generated by feathers cli) that have before create hook named createBlob.

createBlob calle blobs service using bellow code:

      uri: context.data.uri
    }

    const result = await context.app.service('blobs').create(blob)
      .then(function (response) {
        console.log('Blob saved!', JSON.stringify(response.size), " bytes");
        return response;
      }).catch(err => {
        console.log(ErrSavingBlob.message, " ", err.message)
        throw new ErrSavingBlob();
      })

My code for blobs.service (generated by feathers cli) as follows

const {CONST} = require('../../util/const');
const hooks = require('./blobs.hooks');

const blobService = require('feathers-blob');
const fs = require('fs-blob-store');

const blobStorage = fs(CONST.uploadPath);

module.exports = function (app) {
  app.use('/blobs', blobService({
    Model: blobStorage
  }));
  const service = app.service('blobs');
  service.hooks(hooks);
};
HarisHashim commented 5 years ago

Possibly related to #6 and #47

claustres commented 5 years ago

By default if you don't provide an id to be used as a key in the store this module tries to infer the extension from your data URI. Since it is of mime type application/octet-stream this is expected behavior to select a bin extension according to https://github.com/jshttp/mime-db.

If you'd like to keep the extension you should probably try to be more specific in your data URI, eg https://github.com/jshttp/mime-db/blob/master/src/apache-types.json#L1689, or provide a custom ID.

Let us know if it works.

HarisHashim commented 5 years ago

Thanks @claustres for the answer. I don't really understand since I am quite new.

Is there sample code to show how to customize the filename and extension to save? Maybe showing how providing an id is to be done?

It turns out that I submit file extension from frontend in that particular REST call. So it is easy for me to tell the store or feathers blobs what extension should be used.

I also plan to append some info to the saved filename.

Thanks and really appreciate it!

claustres commented 5 years ago

Tests are always a good way to learn or have sample code: https://github.com/feathersjs-ecosystem/feathers-blob/blob/master/test/index.test.js#L87.

HarisHashim commented 5 years ago

Thanks! That sure clear things up.

claustres commented 5 years ago

Reopen if required, thanks.