Skip to main content

File upload

The Spika backend uses a custom file upload mechanism to have better control over the uploading process than it would by using a standard multi-part file upload. The file upload roughly looks like this:

  • The file is divided into chunks on the client side.
  • The chunks are uploaded to the server as a normal POST request with the JSON body.
  • Each chunk has its position in the entire file so that the client can send chunks in parallel.
  • The server returns the list of chunks that are saved successfully so that the client can re-send the chunk that is missing.
  • When all chunks are uploaded, an API is called to concatenate chunks and generate the original file.

Creating chunks

The client divides a file into chunks. Chunks have no specific size – the Spika client divides files into 54 KB per chunk.

Sending chunks

The client calls the /upload/files API and sends all chunks via this API. The request parameters look like this:

chunkBase64 encoded chunk.
offsetThe position of the chunk in a whole chunk sequence.
clientIdThe ID is generated on the client side to distinguish between other upload processes from the same client.

Verifying chunks (finishing the upload)

totalThe total number of chunks
sizeOriginal file size
mimeTypeFile mime type
fileNameFile name
clientIdClient ID, same as the API above.
fileHashHash of the file content. The server returns a 400 error code if the hash is not matched.
typeThe model to which the file belongs. For example, “message”, “avatar”.
relationIdThe unique ID of the model to which the file belongs. For example, a message ID if the file is for a message. The user ID if the file uses an avatar of a user.