-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.txt
More file actions
401 lines (320 loc) · 10.3 KB
/
message.txt
File metadata and controls
401 lines (320 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# ToothFairy4M API Documentation
## Authentication
All API endpoints require authentication through web sessions (exception made for /login/ ofc).
### Login Process
1. **Login URL**: `POST /login/`
2. **Authentication**: Use Django's standard login form with username and password
3. **Session**: After successful login, you'll receive session cookies for subsequent requests
4. **CSRF Protection**: Include CSRF tokens in POST requests (for web form submissions)
### User Roles
The system has different user roles with varying permissions:
- **Standard User**: Can view scans
- **Annotator**: Can upload and annotate scans
- **Administrator**: Full access to all features
- **Student Developer**: Can upload scans, see debug content
- **Demo User**: Limited demo access
### Registration
- **Register URL**: `POST /register/`
- **Invitation System**: New users need invitations
### Authentication Requirements
- Must be logged in with valid session cookies
- Upload permissions required for file uploads (`annotator`, `admin`, or `student_dev` roles)
- Different visibility levels based on user roles
## File Upload API
### Scan Upload
**Endpoint**: `POST /upload/`
**Authentication**: Required (must be logged in with upload permissions)
**Content-Type**: `multipart/form-data`
**Form Fields**:
```
- upper_scan_raw: File (STL file for upper jaw IOS scan)
- lower_scan_raw: File (STL file for lower jaw IOS scan)
- cbct: File (CBCT scan file - NIFTI or DICOM)
- cbct_upload_type: String ('file' or 'folder')
- cbct_folder_files: Multiple files (for DICOM folder uploads)
- folder: String (optional folder assignment)
- name: String (scan pair name)
```
**File Types Supported**:
- **IOS Scans**: STL files for upper and lower jaw scans
- **CBCT**: NIFTI files (.nii, .nii.gz) or DICOM folders
- **Audio**: WebM files for voice captions
**Upload Limits**:
- Maximum file size: 5GB per file
- Maximum number of files: 1500 (for DICOM folders)
**Response**: Redirects to scan list with success/error messages
**Processing Flow**:
1. Files are uploaded and saved to dataset storage
2. Processing jobs are automatically created for uploaded files
3. Jobs run asynchronously to process the uploaded data
4. Processing status can be monitored via Processing Job APIs
### Voice Caption Upload
**Endpoint**: `POST /scan/<scanpair_id>/voice-caption/`
**Parameters**:
- `scanpair_id`: Integer ID of the scan pair
**Form Fields**:
```
- audio_file: File (WebM audio file)
- modality: String (type of modality being described)
```
**Processing**: Audio files are automatically queued for transcription processing
### Text Caption Upload
**Endpoint**: `POST /scan/<scanpair_id>/text-caption/`
**Parameters**:
- `scanpair_id`: Integer ID of the scan pair
**Form Fields**:
```
- text: String (manual text caption)
- modality: String (type of modality being described)
```
## File Download API
### Direct File Serving
**Endpoint**: `GET /api/processing/files/serve/<file_id>/`
**Parameters**:
- `file_id`: Integer ID from FileRegistry
**Authentication**: Required (session-based)
**Response**: Direct file download with appropriate MIME type
- CBCT files: `application/octet-stream`
- STL files: `model/stl`
- Audio files: `audio/webm`
- Other files: Auto-detected MIME type
**Headers**:
```
Content-Disposition: inline; filename="filename.ext"
Content-Type: appropriate/mime-type
```
**Example**:
```bash
curl -H "Cookie: sessionid=your_session_id" \
"http://your-domain/api/processing/files/serve/123/"
```
### File Registry API
**Endpoint**: `GET /api/processing/files/`
**Authentication**: Required
**Query Parameters**:
```
- file_type: String (filter by file type)
- scanpair_id: String (filter by scan pair)
- limit: Integer (pagination limit, default 50)
- offset: Integer (pagination offset, default 0)
```
**Response**:
```json
{
"success": true,
"files": [
{
"id": 123,
"file_type": "cbct_processed",
"file_path": "/path/to/file",
"file_size": 1048576,
"file_hash": "sha256hash",
"created_at": "2024-01-01T12:00:00Z",
"metadata": {},
"scanpair_id": "SCAN001",
"patient_id": "PAT001",
"voice_caption_id": null,
"processing_job_id": 456
}
],
"pagination": {
"total_count": 100,
"limit": 50,
"offset": 0,
"has_more": true
}
}
```
### Scan Data APIs
These endpoints provide access to processed scan data:
**Scan Viewer Data**: `GET /api/scan/<scanpair_id>/data/`
- Returns comprehensive scan data for viewer applications
**CBCT Data**: `GET /api/scan/<scanpair_id>/cbct/`
- Returns CBCT-specific data and metadata
**Panoramic Data**: `GET /api/scan/<scanpair_id>/panoramic/`
- Returns panoramic view data if available
**NIFTI Metadata**: `GET /api/scan/<scanpair_id>/nifti-metadata/`
- Returns NIFTI file metadata and properties
### Health Check
**Endpoint**: `GET /api/processing/health/`
**Response**:
```json
{
"success": true,
"status": "healthy",
"pending_jobs": 5,
"processing_jobs": 2,
"dataset_dir_exists": true,
"dataset_raw_dir_exists": true,
"dataset_processed_dir_exists": true
}
```
## Example Usage
### Complete Upload/Download Workflow
#### Python Example
```python
import requests
import json
# 1. Login
session = requests.Session()
# Get CSRF token first (for web forms)
login_page = session.get('http://your-domain/login/')
csrf_token = session.cookies.get('csrftoken')
login_data = {
'username': 'your_username',
'password': 'your_password',
'csrfmiddlewaretoken': csrf_token
}
login_response = session.post('http://your-domain/login/', data=login_data)
if login_response.status_code == 200:
print("Login successful")
else:
print("Login failed")
# 2. Upload files
files = {
'upper_scan_raw': open('upper.stl', 'rb'),
'lower_scan_raw': open('lower.stl', 'rb'),
'cbct': open('cbct.nii', 'rb')
}
data = {
'name': 'Patient 001 Scan',
'cbct_upload_type': 'file',
'csrfmiddlewaretoken': session.cookies.get('csrftoken')
}
upload_response = session.post('http://your-domain/upload/',
files=files, data=data)
if upload_response.status_code == 200:
print("Upload successful")
else:
print("Upload failed")
# Close files
for file_obj in files.values():
file_obj.close()
# 3. Get file registry
files_response = session.get('http://your-domain/api/processing/files/')
if files_response.status_code == 200:
files_data = files_response.json()
print(f"Found {len(files_data['files'])} files")
else:
print("Failed to get file registry")
# 4. Download a file
if files_data['files']:
file_id = files_data['files'][0]['id']
download_response = session.get(
f'http://your-domain/api/processing/files/serve/{file_id}/'
)
if download_response.status_code == 200:
# Save the downloaded file
with open('downloaded_file', 'wb') as f:
f.write(download_response.content)
print("File downloaded successfully")
else:
print("Download failed")
# 5. Monitor processing jobs
jobs_response = session.get('http://your-domain/api/processing/jobs/')
if jobs_response.status_code == 200:
jobs_data = jobs_response.json()
for job in jobs_data['jobs']:
print(f"Job {job['id']}: {job['status']}")
```
#### cURL Examples
**Login:**
```bash
# Get CSRF token
curl -c cookies.txt http://your-domain/login/
# Login with credentials
curl -b cookies.txt -c cookies.txt \
-d "username=your_username&password=your_password&csrfmiddlewaretoken=TOKEN" \
http://your-domain/login/
```
**Upload files:**
```bash
curl -b cookies.txt \
-F "upper_scan_raw=@upper.stl" \
-F "lower_scan_raw=@lower.stl" \
-F "cbct=@cbct.nii" \
-F "name=Patient 001 Scan" \
-F "cbct_upload_type=file" \
-F "csrfmiddlewaretoken=TOKEN" \
http://your-domain/upload/
```
**Get file registry:**
```bash
curl -b cookies.txt http://your-domain/api/processing/files/
```
**Download file:**
```bash
curl -b cookies.txt -o downloaded_file.nii \
http://your-domain/api/processing/files/serve/123/
```
**Check processing jobs:**
```bash
curl -b cookies.txt http://your-domain/api/processing/jobs/
```
## Error Handling
All API endpoints return JSON error responses with appropriate HTTP status codes:
### HTTP Status Codes
- `200`: Success
- `400`: Bad Request (invalid parameters)
- `401`: Unauthorized (not logged in)
- `403`: Forbidden (insufficient permissions)
- `404`: Not Found
- `500`: Internal Server Error
### Error Response Format
```json
{
"error": "Description of the error"
}
```
### Common Errors
**Authentication Errors:**
```json
{
"error": "Authentication required"
}
```
**Permission Errors:**
```json
{
"error": "You do not have permission to upload scans"
}
```
**File Not Found:**
```json
{
"error": "File not found in registry"
}
```
**Invalid Job Type:**
```json
{
"error": "Invalid job type. Valid types: cbct_processing, ios_processing, audio_transcription"
}
```
**Processing Errors:**
```json
{
"error": "Job not found"
}
```
### Best Practices
1. **Session Management**: Always maintain session cookies across requests
2. **Error Handling**: Check HTTP status codes and parse error messages
3. **File Validation**: Ensure files are in correct format before upload
4. **Progress Monitoring**: Use processing job APIs to monitor upload progress
5. **Resource Cleanup**: Close file handles after uploads
6. **Rate Limiting**: Be respectful of server resources with reasonable request rates
### Configuration
The API behavior can be configured through Django settings:
- **DATASET_PATH**: Base path for file storage
- **DATA_UPLOAD_MAX_MEMORY_SIZE**: Maximum upload size (default: 5GB)
- **FILE_UPLOAD_MAX_MEMORY_SIZE**: Maximum file upload size (default: 5GB)
- **DATA_UPLOAD_MAX_NUMBER_FILES**: Maximum number of files (default: 1500)
### Security Considerations
1. **HTTPS**: Use HTTPS in production for secure data transmission
2. **Authentication**: All endpoints require valid authentication
3. **CSRF Protection**: Web form uploads require CSRF tokens
4. **File Validation**: Uploaded files are validated for type and content
5. **Access Control**: Role-based permissions control file access
6. **Session Security**: Sessions use secure cookies in production
This API provides a complete system for authenticated file uploads and downloads with processing job tracking for the ToothFairy4M dental scan management system.