Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,16 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
-
import boto3
|
| 3 |
-
from botocore.client import Config
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
-
import mimetypes
|
| 7 |
from werkzeug.utils import secure_filename
|
| 8 |
import uuid
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
# Internet Archive S3 Configuration
|
| 13 |
IA_ACCESS_KEY = "YXzY5OrREXLL6XBh"
|
| 14 |
IA_SECRET_KEY = "m2XnL3X7xCB1pNGE"
|
| 15 |
-
IA_ENDPOINT = "https://s3.us.archive.org"
|
| 16 |
-
IA_BUCKET = "your-bucket-name" # Replace with your IA bucket name
|
| 17 |
-
|
| 18 |
-
# Initialize S3 client
|
| 19 |
-
s3_client = boto3.client(
|
| 20 |
-
's3',
|
| 21 |
-
endpoint_url=IA_ENDPOINT,
|
| 22 |
-
aws_access_key_id=IA_ACCESS_KEY,
|
| 23 |
-
aws_secret_access_key=IA_SECRET_KEY,
|
| 24 |
-
config=Config(signature_version='s3v4')
|
| 25 |
-
)
|
| 26 |
|
| 27 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg'}
|
| 28 |
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
|
|
@@ -410,34 +398,52 @@ def upload_file():
|
|
| 410 |
if not allowed_file(file.filename):
|
| 411 |
return jsonify({'success': False, 'error': 'Invalid file type'}), 400
|
| 412 |
|
| 413 |
-
# Generate unique
|
| 414 |
-
original_filename = secure_filename(file.filename)
|
| 415 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 416 |
unique_id = str(uuid.uuid4())[:8]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
filename = f"{timestamp}_{unique_id}_{original_filename}"
|
| 418 |
|
| 419 |
-
#
|
| 420 |
-
|
|
|
|
| 421 |
|
| 422 |
-
# Upload
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
'ACL': 'public-read'
|
| 430 |
-
}
|
| 431 |
-
)
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
except Exception as e:
|
| 443 |
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
from werkzeug.utils import secure_filename
|
| 5 |
import uuid
|
| 6 |
+
import io
|
| 7 |
+
from internetarchive import get_item
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
# Internet Archive S3 Configuration
|
| 12 |
IA_ACCESS_KEY = "YXzY5OrREXLL6XBh"
|
| 13 |
IA_SECRET_KEY = "m2XnL3X7xCB1pNGE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg'}
|
| 16 |
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
|
|
|
|
| 398 |
if not allowed_file(file.filename):
|
| 399 |
return jsonify({'success': False, 'error': 'Invalid file type'}), 400
|
| 400 |
|
| 401 |
+
# Generate unique identifier for this upload batch
|
|
|
|
| 402 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 403 |
unique_id = str(uuid.uuid4())[:8]
|
| 404 |
+
identifier = f"upload_{timestamp}_{unique_id}"
|
| 405 |
+
|
| 406 |
+
# Get the item (will be created if doesn't exist)
|
| 407 |
+
item = get_item(identifier)
|
| 408 |
+
|
| 409 |
+
# Generate unique filename
|
| 410 |
+
original_filename = secure_filename(file.filename)
|
| 411 |
filename = f"{timestamp}_{unique_id}_{original_filename}"
|
| 412 |
|
| 413 |
+
# Read file into BytesIO
|
| 414 |
+
file_data = io.BytesIO(file.read())
|
| 415 |
+
file_data.name = filename
|
| 416 |
|
| 417 |
+
# Upload using internetarchive library with credentials
|
| 418 |
+
metadata = {
|
| 419 |
+
'mediatype': 'image',
|
| 420 |
+
'collection': 'opensource', # Use 'opensource' or your collection
|
| 421 |
+
'title': f'Image Upload {timestamp}',
|
| 422 |
+
'description': 'Image uploaded via API'
|
| 423 |
+
}
|
|
|
|
|
|
|
|
|
|
| 424 |
|
| 425 |
+
response = item.upload(
|
| 426 |
+
file_data,
|
| 427 |
+
metadata=metadata,
|
| 428 |
+
access_key=IA_ACCESS_KEY,
|
| 429 |
+
secret_key=IA_SECRET_KEY,
|
| 430 |
+
verbose=False,
|
| 431 |
+
queue_derive=False
|
| 432 |
+
)
|
| 433 |
|
| 434 |
+
# Check if upload was successful
|
| 435 |
+
if response and len(response) > 0:
|
| 436 |
+
# Generate public URL
|
| 437 |
+
file_url = f"https://archive.org/download/{identifier}/{filename}"
|
| 438 |
+
|
| 439 |
+
return jsonify({
|
| 440 |
+
'success': True,
|
| 441 |
+
'url': file_url,
|
| 442 |
+
'filename': filename,
|
| 443 |
+
'identifier': identifier
|
| 444 |
+
})
|
| 445 |
+
else:
|
| 446 |
+
return jsonify({'success': False, 'error': 'Upload failed - no response'}), 500
|
| 447 |
|
| 448 |
except Exception as e:
|
| 449 |
return jsonify({'success': False, 'error': str(e)}), 500
|