function store_uploaded_document
Stores uploaded document metadata and content in a thread-safe application state dictionary, organized by user email and document ID.
/tf/active/vicechatdev/vice_ai/complex_app.py
181 - 193
simple
Purpose
This function provides a centralized mechanism for storing uploaded documents in memory. It maintains a nested dictionary structure where documents are organized by user email, then by document ID. The function uses thread locks to ensure thread-safe access to the shared application state, making it suitable for multi-threaded web applications. It stores document name, content, file type, and upload timestamp, and logs the storage operation for auditing purposes.
Source Code
def store_uploaded_document(user_email, document_id, name, content, file_type):
"""Store uploaded document content"""
with app_state['locks']['uploaded_documents']:
if user_email not in app_state['uploaded_documents']:
app_state['uploaded_documents'][user_email] = {}
app_state['uploaded_documents'][user_email][document_id] = {
'name': name,
'content': content,
'file_type': file_type,
'upload_time': datetime.now().isoformat()
}
logger.info(f"Stored uploaded document: {name} for user {user_email}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
document_id |
- | - | positional_or_keyword |
name |
- | - | positional_or_keyword |
content |
- | - | positional_or_keyword |
file_type |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the email address of the user uploading the document. Used as the primary key to organize documents by user in the app_state dictionary.
document_id: Unique identifier (typically a UUID string) for the document. Used as the secondary key to uniquely identify each document within a user's collection.
name: String representing the original filename or display name of the uploaded document. Stored for reference and display purposes.
content: The actual content of the uploaded document. Can be binary data, text, or any serializable format depending on the file type. No type constraints are enforced.
file_type: String indicating the MIME type or file extension of the uploaded document (e.g., 'pdf', 'docx', 'text/plain'). Used to identify how to process or display the document.
Return Value
This function does not return any value (implicitly returns None). It performs a side effect by modifying the global app_state dictionary and logging the operation.
Dependencies
datetimelogging
Required Imports
from datetime import datetime
import logging
Usage Example
import logging
import threading
from datetime import datetime
# Initialize required global state
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
app_state = {
'locks': {
'uploaded_documents': threading.Lock()
},
'uploaded_documents': {}
}
# Define the function
def store_uploaded_document(user_email, document_id, name, content, file_type):
with app_state['locks']['uploaded_documents']:
if user_email not in app_state['uploaded_documents']:
app_state['uploaded_documents'][user_email] = {}
app_state['uploaded_documents'][user_email][document_id] = {
'name': name,
'content': content,
'file_type': file_type,
'upload_time': datetime.now().isoformat()
}
logger.info(f"Stored uploaded document: {name} for user {user_email}")
# Usage example
user_email = 'user@example.com'
document_id = 'doc-12345'
name = 'report.pdf'
content = b'PDF binary content here'
file_type = 'application/pdf'
store_uploaded_document(user_email, document_id, name, content, file_type)
# Verify storage
print(app_state['uploaded_documents'][user_email][document_id]['name'])
Best Practices
- Ensure app_state dictionary is properly initialized before calling this function
- Always use unique document_id values (e.g., UUID) to prevent overwriting existing documents
- Consider implementing size limits on content to prevent memory exhaustion
- This stores documents in memory; for production use, consider persistent storage solutions
- The function does not validate input parameters; implement validation before calling
- Thread lock ensures safety but may become a bottleneck under high concurrency; consider using finer-grained locks per user
- No cleanup mechanism exists; implement document expiration or cleanup routines to prevent unbounded memory growth
- Consider encrypting sensitive document content before storage
- The upload_time uses ISO format for easy serialization and parsing
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function store_uploaded_document_v1 90.2% similar
-
function store_document 88.6% similar
-
function get_uploaded_document 80.5% similar
-
function get_uploaded_document_v1 79.7% similar
-
function get_user_uploaded_documents 77.0% similar