File services with Dreamfactory (file creation)


This and the following posts will cover the areas about the file API that was not clearly covered in the Dreamfactory Documentation. The focus on this article is about file & folder creation.

Contents

  1. Create a file
    1. Set content via JSON Request Body
    2. Set content via Multi-part form upload (file upload)
    3. Download to server from URL
  2. Create a folder
  3. Combine folder and file creation

General Information

For all these requests, a JSON request body is used in the following basic format.

POST : Each element of resource array will define a File, Folder request.

  • type : file or folder
  • name : name of file
  • path : path to file
  • content_type : media type ie: text/json
  • content : contents of the file
{
    "resource": [
        {
            "name": "folder2",
            "path": "folder2",
            "type":"folder"
        },
        {
            "name": "folder2/file1.txt",
            "path": "folder2/file1.txt",
            "type":"file",
            "content_type": "text",
            "content":"gdfgdgdfgf"
        }
    ]
}

1. Create a file

1.1 One or multiple files with JSON data.

Type : POST

Note: Setting property “is_base64”: true will enable to upload images with “content” set as Base 64 encoded string.

Warning: Using Base64 encoding for large images is highly discouraged!!! (I tried uploading 18MP image and it was not worth the trouble). For these cases, go for direct file upload by multi-part form.

{
    "resource": [
        {
            "name": "folder2/file1.txt",
            "path": "folder2/file1.txt",
            "type":"file",
            "content_type": "text",
            "content":"gdfgdgdfgf"
        },
        {
            "name": "folder2/file2.txt",
            "path": "folder2/file2.txt",
            "type":"file",
            "content_type": "text",
            "content":"lsjfvgm"
        }
    ]
}

This will create two files in the folder names “folder2”. If the folder does not exist, an error will occur.

1.2 Multi-part form upload (file upload)

When I tried to upload some big images, base 64 encoding was not good for the performance at any level. Therefore direct file upload was the best option for my scenario. Unfortunately this was not clearly documented in the wiki.

Type : POST (multi part form)

I have tested and used the following three methods.

Method 1. Plain HTML + minimum Javascript Based.

This can be done in a few ways. Below example is from “test_rest.html” in dreamfactory to test REST calls. Of course the javascript can be completely dropped out if needed.

HTML

<form enctype="multipart/form-data" onsubmit="postForm(this)" action="/api/v2/system/user/" method="POST">
 <input type="hidden" name="app_name" value="admin" />
 <!-- MAX_FILE_SIZE must precede the file input field -->
 <input type="hidden" name="MAX_FILE_SIZE" value="3000000000000" />
 <!-- Name of input element determines name in $_FILES array -->
 Test importing users via file: <br/>
 <input name="files" type="file" />
 <br/>
 <br/>
 <input type="submit" value="Send File" />
 </form>

JS

function postForm(form){
    var jwt = $('#token').val(); //Session Token
    var apiKey= $('#app').val(); // API Key for app
    var url = $('#url').val(); //url for the REST call
    form.action = url+'?session_token='+jwt+"&api_key="+apiKey;
    // the token and api key can be sent as headers (if going for AJAX call)
}

Method 2. JQuery AJAX Based.

TODO

Method 3. Java (okHTTP) based

This method employs the already given Java SDK example for Dreamfactory. I have modified ImageUtils.java, ImageServices.java for my purpose. Original link https://github.com/dreamfactorysoftware/android-sdk

Note: This method was done to use on Java 8 on a PC. The method for Android is different and can be found in the api info.

ImageUtils.java

public void addImageFromLocalFile(String fileServiceName, String imageDir, String imageName, File imgFile, Callback<FileRecord> callBackFileRecord) {

 RequestBody requestBody = new MultipartBody.Builder()
 .setType(MultipartBody.FORM)
 .addFormDataPart("files", "imageName-1.png",
 RequestBody.create(MediaType.parse("image/png"), imgFile))
 .build();
 final ImageService imageService = DreamFactoryAPI.getInstance(App.SESSION_TOKEN).getService(ImageService.class);

 imageService.addLocalImage(fileServiceName, imageDir, imageName, requestBody).enqueue(callBackFileRecord);
 }

ImageService.java

@POST("{file_service_name}/{id}/{name}")
Call<FileRecord> addLocalImage(@Path(value = "file_service_name") 
 String fileServiceName, @Path(value = "id") 
 String contactId, @Path(value = "name") 
 String name, @Body RequestBody file);

1.3 Download to server from URL

This method is quite straightforward and explained in Dreamfactory Wiki;

http://wiki.dreamfactory.com/DreamFactory/Tutorials/Uploading_File#Example_-_Upload_a_JPEG_image_to_a_directory_called_images_using_storage_service_called_.27files.27

2. Create Folder

Creating a directory is similar to creating a file, the format is called FolderRequest. The difference is, you will be calling a directory in the API call. From the looks of it, this trick may work without specifying the exact folder, etc!!!

{
    "resource": [
        {
            "name": "folder2",
            "path": "folder2",
            "type":"folder"
        }
    ]
}

3. Combine Folder and File Creation

Same URL format in the above case, the only difference is you can ask Dreamfactory to create a folder and put files into it. First the file to be created must be specified, then the files to place inside the folder.

{
    "resource": [
        {
            "name": "folder2",
            "path": "folder2",
            "type":"folder"
        },
        {
            "name": "folder2/file1.txt",
            "path": "folder2/file1.txt",
            "type":"file",
            "content_type": "text",
            "content":"gdfgdgdfgf"
        }
    ]
}

Conclusion

The file API is quite nice and create a layer between the filesystem and our applications. You can easily switch to a cloud based storage or a different network drive without the users noticing it.

 

Advertisements

Dreamfactory – API Automation!


I came across Dreamfactory while I and a colleague of mine were searching for a REST API for PHP. In summary, this framework simplified a lot of setting up and development time! Specially this is an open source project and has enterprise support.

The most valuable feature I saw is ability to connect to almost all major database types and automatically generate the REST API calls. On top of that, this system offers role based authentication and a lot of features.

What you can do with Dreamfactory;

  • Connect to a database and get all necessary REST calls
  • User management, role based authentication, application level access control
  • Custom server side scripting with v8JS, PHP, Node.JS, Python
  • Auto generated API documentation with “try now” option (Based on swagger)

Setting up this framework need some practice and experience with the command line, however following the wiki articles will certainly do the job.

Performance : not so much! Depending on the server, the time to process a REST call may take up to a half a second or more.

You can try Dreamfactory on their trial accounts or you can clone the Git repo and set it up on a local machine or a hosted server.