Search code examples
databasepostsavestoredreamfactory

DreamFactory: How to upload an image to the fileserver and save the path of the image in DataBase?


I'm thinking about using DreamFactory as API. I'm fresh in serversice-logic. I want to store an image on the filesystem and the path of the stored file into a database. I won't store the image as blob inside the database, because their will be many other images following. Can anybody please point me into the right direction?

First post the image to the filesystem using:

post /files/{file_path} 

And after that:

post /mysql/_table/{table_name} createRecords() - Create one or more records

But how to pass the path to the database?


Solution

  • One way would be to use server-side scripting (V8Js in this case) on the POST to /files/{file_path}. Go to the Admin application, Scripts tab, and work your way through the events to the following path...

    Process Event Scripts > files > files.{file_path} > [POST] files.{file_path} > files.{file_path}.post.post_process
    

    Try the following script...

    // event.resource is the {file_path} part, 
    // including any folders from root, i.e. my/path/file.txt
    var_dump(event.resource); 
    
    // Create a record that you want to post to a table, 
    // using the "todo" table for example, setting "name" to the file path
    // "mysql" is my db service name
    var record = {"resource": [ {"name": event.resource, "complete": false } ] }; 
    var result = platform.api.post('mysql/_table/todo', record);
    
    var_dump(result); 
    // result contains a resource array with the id of the record created
    // like {"resource": [ {"id": 5} ] }