5.0 KiB
5.0 KiB
Mobile Integration Guide
This document outlines the requirements for mobile applications to integrate with the Simple File Upload Server.
API Endpoints
Upload File
Endpoint: POST /upload
Content-Type: multipart/form-data
Request Parameters:
file: File (required) - The file to upload
max_downloads: Integer (optional) - Maximum number of downloads allowed (default: 1)
expiry_hours: Integer (optional) - Hours until file expires (default: 24, max: 24)
Example Request (Swift):
let url = URL(string: "http://your-server:7777/upload")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var body = Data()
// Add file data
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"file\"; filename=\"log.txt\"\r\n")
body.append("Content-Type: text/plain\r\n\r\n")
body.append(fileData)
body.append("\r\n")
// Add parameters
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"max_downloads\"\r\n\r\n")
body.append("3\r\n")
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"expiry_hours\"\r\n\r\n")
body.append("24\r\n")
body.append("--\(boundary)--\r\n")
request.httpBody = body
Example Request (Kotlin):
val file = File("log.txt")
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"file",
file.name,
file.asRequestBody("application/octet-stream".toMediaType())
)
.addFormDataPart("max_downloads", "3")
.addFormDataPart("expiry_hours", "24")
.build()
val request = Request.Builder()
.url("http://your-server:7777/upload")
.post(requestBody)
.build()
Success Response:
{
"code": "ABC123"
}
Error Responses:
{
"error": "No file part"
}
Status: 400 Bad Request
{
"error": "No selected file"
}
Status: 400 Bad Request
Download File
Endpoint: GET /download/{code}
Parameters:
code: 6-character alphanumeric code received from upload
Example Request (Swift):
let code = "ABC123"
let url = URL(string: "http://your-server:7777/download/\(code)")!
let task = URLSession.shared.downloadTask(with: url) { localURL, response, error in
guard let localURL = localURL else { return }
// Handle downloaded file
}
task.resume()
Example Request (Kotlin):
val code = "ABC123"
val request = Request.Builder()
.url("http://your-server:7777/download/$code")
.get()
.build()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
// Handle downloaded file
}
override fun onFailure(call: Call, e: IOException) {
// Handle error
}
})
Success Response:
- Status: 200 OK
- Content-Type: application/octet-stream
- Content-Disposition: attachment; filename="original_filename.ext"
Error Responses:
- Status: 404 Not Found
- Content-Type: application/json
Various error messages:
{
"error": "Invalid or expired code"
}
{
"error": "File expired"
}
{
"error": "Download limit reached"
}
{
"error": "File not found"
}
Implementation Requirements
File Upload
- Implement proper error handling for network issues
- Show upload progress to user
- Handle timeout scenarios (recommended timeout: 60 seconds)
- Store received code securely if needed
- Validate file size before upload (max size: 16MB)
- Implement retry logic for failed uploads
File Download
- Handle all error scenarios gracefully
- Show download progress to user
- Validate downloaded file integrity
- Implement proper file saving logic
- Handle interrupted downloads with resume capability if possible
Security Considerations
- Use HTTPS for all API calls in production
- Don't store sensitive files for extended periods
- Implement proper error messages without exposing server details
- Sanitize all user inputs
- Handle session timeouts appropriately
Testing Requirements
- Test with various file types and sizes
- Verify all error scenarios
- Test network condition variations:
- Slow network
- Intermittent connectivity
- Network switches (WiFi to Cellular)
- Test concurrent uploads/downloads
- Verify proper cleanup of temporary files
Sample Status Codes
- 200: Success
- 400: Bad Request (invalid parameters)
- 404: Not Found (invalid code or expired file)
- 413: Payload Too Large (file size exceeded)
- 500: Server Error
Rate Limiting
- Maximum 10 requests per minute per IP
- Implement exponential backoff for retries
Migration Notes
If upgrading from previous versions:
- Update API endpoint URLs
- Implement new error handling
- Update file size limits
- Add support for download codes
- Remove old URL-based download logic