This document specifies the implementation requirements for the new Attachment Manifest API endpoint that enables efficient, version-based synchronization of attachments between clients and the Synkronus server.
The current attachment synchronization approach requires clients to scan all observation data to determine which attachments to download, which doesn't scale well. The new approach uses the existing version-based synchronization system to provide incremental attachment manifests.
Purpose: Returns a manifest of attachment operations (download/delete) that need to be performed since a specified data version.
Authentication: Bearer token required (read-only or read-write access)
Request Body:
{
"client_id": "mobile-app-123",
"since_version": 42
}Response Body:
{
"current_version": 45,
"operations": [
{
"operation": "download",
"attachment_id": "abc123-def4-5678-9012-345678901234.jpg",
"download_url": "https://api.example.com/attachments/abc123-def4-5678-9012-345678901234.jpg",
"size": 524288,
"content_type": "image/jpeg",
"version": 43
},
{
"operation": "delete",
"attachment_id": "old456-file7-8901-2345-678901234567.png",
"version": 44
}
],
"total_download_size": 524288,
"operation_count": {
"download": 1,
"delete": 1
}
}The implementation should leverage the existing version-based synchronization system. You'll need to track attachment operations alongside observation changes.
Recommended approach:
- Use the existing
sync_versiontable for global version tracking - Create an
attachment_operationstable to track attachment changes:
CREATE TABLE attachment_operations (
id SERIAL PRIMARY KEY,
attachment_id VARCHAR(255) NOT NULL,
operation VARCHAR(10) NOT NULL CHECK (operation IN ('create', 'update', 'delete')),
client_id VARCHAR(255), -- NULL for operations affecting all clients
version BIGINT NOT NULL,
size INTEGER, -- NULL for delete operations
content_type VARCHAR(255), -- NULL for delete operations
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX idx_attachment_operations_version ON attachment_operations(version);
CREATE INDEX idx_attachment_operations_client ON attachment_operations(client_id);Query Logic:
- Get all attachment operations where
version > since_version - Filter by
client_id(include both client-specific and global operations) - For each attachment, return only the latest operation
- Generate download URLs for
create/updateoperations - Return
deleteoperations for attachments that were deleted
Version Management:
- Use the same version incrementing system as observations
- Increment global version when attachment operations are recorded
- Ensure atomicity between observation and attachment version updates
Request Validation:
client_id: Required, non-empty stringsince_version: Required, non-negative integer
Response Generation:
current_version: Current global version fromsync_versiontableoperations: Array of operations to performtotal_download_size: Sum of sizes for all download operationsoperation_count: Count of operations by type
Error Handling:
- 400: Invalid request parameters
- 401: Authentication required
- 500: Internal server error
Indexing:
- Index on
versionfor efficient range queries - Index on
client_idfor client filtering - Consider composite index on
(version, client_id)
Pagination:
- For large result sets, consider implementing pagination
- Use
limitandoffsetparameters if needed
Caching:
- Consider caching manifest responses for frequently requested version ranges
- Cache invalidation when new attachment operations are recorded
When observations are created/updated/deleted:
- Extract attachment references from observation data
- Compare with existing attachments to determine operations
- Record attachment operations in
attachment_operationstable - Ensure version consistency between observations and attachments
When attachments are uploaded via PUT /attachments/{attachment_id}:
- Record
createoperation inattachment_operationstable - Increment global version
- Associate with appropriate
client_idif applicable
When attachments are deleted:
- Record
deleteoperation inattachment_operationstable - Increment global version
- Clean up physical files as appropriate
The client will:
- Store
@last_attachment_versionin local storage - Call
/attachments/manifestwithsince_versionparameter - Process returned operations (download/delete)
- Update
@last_attachment_versionafter successful processing
- Request validation
- Response generation
- Version filtering logic
- Operation deduplication
- End-to-end attachment sync flow
- Version consistency between observations and attachments
- Client-specific vs global operations
- Error handling scenarios
- Large manifest generation
- Concurrent client requests
- Database query performance
Access Control:
- Ensure clients can only access their own attachment manifests
- Validate client_id against authenticated user
- Implement rate limiting for manifest requests
Data Privacy:
- Don't expose attachment content in manifest
- Ensure download URLs are properly authenticated
- Consider signed URLs for enhanced security
- Database migration for
attachment_operationstable - Implement endpoint handler
- Add business logic for manifest generation
- Update existing attachment upload/delete flows
- Add monitoring and logging
- Update API documentation
- Deploy and test in staging environment
- Regenerate client SDKs
- Client requests manifest:
POST /attachments/manifestwithsince_version: 42 - Server queries operations: Find all operations where
version > 42 - Server filters by client: Include client-specific and global operations
- Server deduplicates: For each attachment, return only latest operation
- Server generates URLs: Create download URLs for
downloadoperations - Server returns manifest: Complete manifest with operations and metadata
- Client processes operations: Download new files, delete removed files
- Client updates version: Store new
current_versionfor next sync
This implementation provides a scalable, efficient solution for attachment synchronization that leverages the existing version-based sync infrastructure.