function get_uploaded_document
Retrieves a specific uploaded document from the application state for a given user and document ID, returning document metadata and content in a thread-safe manner.
/tf/active/vicechatdev/vice_ai/complex_app.py
200 - 211
simple
Purpose
This function provides thread-safe access to uploaded documents stored in the application's global state. It looks up a document by user email and document ID, returning the document's name, content, file type, and upload time. The function uses a lock to ensure thread-safe access to the shared uploaded_documents dictionary, making it suitable for multi-threaded Flask applications. Returns None if the user or document is not found.
Source Code
def get_uploaded_document(user_email, document_id):
"""Get specific uploaded document"""
with app_state['locks']['uploaded_documents']:
if user_email in app_state['uploaded_documents'] and document_id in app_state['uploaded_documents'][user_email]:
doc_info = app_state['uploaded_documents'][user_email][document_id]
return {
'name': doc_info['name'],
'content': doc_info['content'],
'file_type': doc_info['file_type'],
'upload_time': doc_info['upload_time']
}
return None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
document_id |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the email address of the user who uploaded the document. Used as the primary key to look up the user's documents in the app_state dictionary.
document_id: String or UUID representing the unique identifier of the document to retrieve. Used as the secondary key to look up the specific document within the user's document collection.
Return Value
Returns a dictionary containing document information with keys: 'name' (document filename), 'content' (document content as string or bytes), 'file_type' (file extension or MIME type), and 'upload_time' (timestamp of upload). Returns None if the user_email is not found in app_state or if the document_id does not exist for that user.
Dependencies
threading
Required Imports
from threading import Lock
Usage Example
from threading import Lock
# Initialize app_state (typically done at application startup)
app_state = {
'locks': {
'uploaded_documents': Lock()
},
'uploaded_documents': {
'user@example.com': {
'doc-123': {
'name': 'report.pdf',
'content': b'PDF content here',
'file_type': 'pdf',
'upload_time': '2024-01-15 10:30:00'
}
}
}
}
# Retrieve a document
document = get_uploaded_document('user@example.com', 'doc-123')
if document:
print(f"Document name: {document['name']}")
print(f"File type: {document['file_type']}")
print(f"Uploaded at: {document['upload_time']}")
else:
print("Document not found")
Best Practices
- Ensure app_state is properly initialized before calling this function
- The function uses a lock for thread-safety; avoid holding the lock for extended periods in calling code
- Always check if the return value is None before accessing document properties
- The function returns a new dictionary rather than a reference to the stored document, preventing external modification of app_state
- Consider implementing error handling for malformed app_state structure in production code
- Document IDs should be unique per user to avoid collisions
- The function does not validate user_email format or document_id format; validation should be done before calling
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_uploaded_document_v1 95.1% similar
-
function get_user_uploaded_documents 84.3% similar
-
function store_uploaded_document 80.5% similar
-
function get_user_documents 80.1% similar
-
function store_uploaded_document_v1 76.2% similar