File Upload Functions
Parse multipart form data and upload files to SolidB blob storage. Handle file uploads with metadata preservation and easy retrieval.
set_solidb_address(addr)
Configures the global SoliDB address for upload functions. Call this once during app initialization.
addr(String) - SoliDB server address (e.g., "http://localhost:8529")
# Call once during app startup
set_solidb_address("http://localhost:8529")
parse_multipart(req)
Parses multipart/form-data from a request and extracts file information.
req(Hash) - Request hash with body and headers
filename, content_type, size, data_base64, field_name
let files = parse_multipart(req)
for file in files {
println("Uploaded: " + file["filename"] + " (" + str(file["size"]) + " bytes)")
}
upload_to_solidb(req, collection, field_name)
Uploads a single file from multipart form data directly to SolidB blob storage. Uses the global SoliDB address set by set_solidb_address().
req(Hash) - Request hashcollection(String) - SolidB collection namefield_name(String) - Form field name to upload
blob_id, filename, size, content_type
let result = upload_to_solidb(req, "uploads", "avatar")
if has_key(result, "blob_id") {
println("Uploaded: " + result["filename"])
println("Blob ID: " + result["blob_id"])
}
upload_all_to_solidb(req, collection)
Uploads all files from multipart form data to SolidB. Uses the global SoliDB address set by set_solidb_address().
req(Hash) - Request hashcollection(String) - SolidB collection name
let results = upload_all_to_solidb(req, "uploads")
for result in results {
if has_key(result, "blob_id") {
println("Uploaded: " + result["filename"])
} else {
println("Failed: " + result["error"])
}
}
get_blob_url(collection, blob_id, base_url?, expires_in?)
Generates a URL for downloading a blob from SolidB. If base_url is not provided, uses the global SoliDB address set by set_solidb_address().
collection(String) - SolidB collection nameblob_id(String) - Blob ID returned from uploadbase_url(String, optional) - Base URL for SolidB server. Uses global address if not provided.expires_in(Int, optional) - Expiration time in seconds
# Using global address
let url = get_blob_url("avatars", blob_id)
# With explicit base_url
let url = get_blob_url("avatars", blob_id, "http://localhost:8529")
SolidB Blob Methods
Methods available on Solidb instances for blob storage.
solidb.store_blob(collection, data_base64, filename, content_type)
Stores a file as a blob in SolidB.
collection(String) - Collection namedata_base64(String) - File content as base64filename(String) - Original filenamecontent_type(String) - MIME type
Returns: String - Unique blob ID
let db = Solidb("localhost:5678", "myapp")
let blob_id = db.store_blob("avatars", image_data_base64, "photo.jpg", "image/jpeg")
solidb.get_blob(collection, blob_id)
Retrieves a blob from SolidB.
Returns: String - File content as base64
let image_data = db.get_blob("avatars", blob_id)
solidb.get_blob_metadata(collection, blob_id)
Gets metadata for a blob without fetching the data.
Returns: Hash with _key, filename, content_type, size, created_at
solidb.delete_blob(collection, blob_id)
Deletes a blob from SolidB.
Returns: String - "OK" on success
Complete Example
# App initialization (call once)
set_solidb_address("http://localhost:8529")
# Upload controller
def upload_avatar(req)
let collection = "avatars"
let result = upload_to_solidb(req, collection, "avatar")
if has_key(result, "error") {
return {
"status": 400,
"body": json_stringify({ "error": result["error"] })
}
}
let blob_url = get_blob_url(collection, result["blob_id"])
{
"status": 201,
"body": json_stringify({
"message": "File uploaded successfully",
"blob_id": result["blob_id"],
"filename": result["filename"],
"size": result["size"],
"content_type": result["content_type"],
"url": blob_url
})
}
end
# Get file controller
def get_file(req)
let blob_id = req["params"]["id"]
let db = Solidb("localhost:8529", "myapp")
let metadata = db.get_blob_metadata("avatars", blob_id)
if metadata["_key"] == null {
return { "status": 404, "body": "Not found" }
}
let data = db.get_blob("avatars", blob_id)
{
"status": 200,
"headers": {
"Content-Type": metadata["content_type"],
"Content-Length": str(metadata["size"]),
"Content-Disposition": "attachment; filename=\"" + metadata["filename"] + "\""
},
"body": data
}
end