loopback-component-storage: preferred way to saved to custom file?

568 views
Skip to first unread message

B Lyon

unread,
Jul 17, 2014, 9:37:46 PM7/17/14
to loopb...@googlegroups.com
I've been looking through the newish loopback-component-storage stuff, and it wasn't clear to me what the "proper" way is to save to a filename determined on the server (e.g., if it were based on user somehow, based on id's in db, etc.). The sample uses the same filename as it was on the client machine. Same might go for other methods (aws, etc.) but I didn't look at those yet.

The actual saving and filename determination seemed to be somewhat buried within the library itself without an obvious way to hook into this, but I could have missed something obvious.

thanks!

Sven Woldt

unread,
Jul 19, 2014, 3:57:11 PM7/19/14
to loopb...@googlegroups.com
Hey B Lyon,
I encountered the same problem. The save method is located in the storage-service.js. I did not change those sources as well. Even though that would be an option.
Another way, that worked for me is to intercept the angular-fileupload event "completed" and do your stuff inside that method.
Hope that helps let me know.
I also would like to know if the component storage is gonna be improved in that regard.
Sven

Op vrijdag 18 juli 2014 03:37:46 UTC+2 schreef B Lyon:

brad...@gmail.com

unread,
Jul 19, 2014, 4:44:36 PM7/19/14
to Sven Woldt, loopb...@googlegroups.com
Thanks for the info Sven.

Isn't that angular method that catches that event a client-side event? I am not at my computer right now so I can't doublecheck - I am new to angular as well

Ideally the other loopback passport third-party stuff can be hooked into this with no need to do custom stuff independent of that (from my point of view). Especially since you would want to use the rest API to be able to do this independent of angular.  Would be great to just modify that upload sample to have a loopback-based third-party login that saves things for the logged in user to a custom user-dependent place/name not equal to the source file name as a demo

Sent from my iPhone
--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/xFhW8JVokH4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sven Woldt

unread,
Jul 19, 2014, 5:10:37 PM7/19/14
to loopb...@googlegroups.com, svn.s...@gmail.com
Hey well of course the upload is a client side event.
Look the code:

    uploader.bind "complete", (event, xhr, item, response) ->
      filename = "api/containers/container1/download" + item.file.name
      User.updateOrCreate(imagelocation: filename, callback)

You can see here that angular-fileupload gives you the file in a variable called item.
Before handing it further down the chain manipulate the item.filename if you wish or associate it with a reference like imagelocation for the user.
That is what I hacked together lately. Its not ideal. However, in the documentation for the component storage you can find also methods like upload stream or download stream. If you wanna play around saving records directly into the db.
Hope this will help you out.
Kind regards,
Sven

B Lyon

unread,
Jul 19, 2014, 8:10:10 PM7/19/14
to Sven Woldt, loopb...@googlegroups.com
More thinking out loud

If you're using passport and the user has authenticated, the user will
be attached to the request already for you when you're in this method
in storage-handler.js

exports.upload = function (provider, req, res, container, cb)

This means you can perhaps just have methods on the user object to
get the custom filename to use, tell it the file was saved
successfully, etc. The upload method would first check if the method
exists, of course

For download, the relevant id in the database of the upload would be
in the passed in request (e.g., if they clicked on the button that
passes that id), and the download method could then just ask the user
object what the filename is for that id (and check that it is
associated with the current user, etc.)
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "LoopbackJS" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/loopbackjs/xFhW8JVokH4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> loopbackjs+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
BF Lyon
http://www.nowherenearithaca.com
Reply all
Reply to author
Forward
0 new messages