feat: UPLOADING WORKING FINALLY!!!

This commit is contained in:
Kentai Radiquum 2025-02-17 20:06:12 +05:00
parent 6063dc1401
commit d99a1e0214
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
4 changed files with 171 additions and 22 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@
.env
node_modules
__pycache__
*firebase-adminsdk*.json
temp

View file

@ -1,33 +1,157 @@
import json
import PIL
import PIL.Image
import boto3.session
from flask import Flask, render_template, request
from flask import Flask, render_template, request, Response
from werkzeug.utils import secure_filename
from dotenv import load_dotenv
import os
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError, SSLError
import firebase_admin
from firebase_admin import firestore, credentials, exceptions
load_dotenv()
app = Flask(__name__)
UPLOAD_FOLDER = "./temp"
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
session = boto3.session.Session()
s3 = session.client(
service_name='s3',
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
endpoint_url=os.getenv('AWS_ENDPOINT'),
app = Flask(__name__)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
s3 = boto3.client(
service_name="s3",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
endpoint_url=os.getenv("AWS_ENDPOINT"),
config=Config(s3={"request_checksum_calculation": "when_required"}),
)
@app.route('/')
cred = credentials.Certificate(os.getenv("FIREBASE_SERVICE_ACCOUNT"))
firebase_admin.initialize_app(cred)
db = firestore.client()
@app.route("/")
def Home():
from urllib.request import urlopen
print(urlopen("https://www.howsmyssl.com/a/check").read())
# objects = s3.list_objects(Bucket=os.getenv("AWS_BUCKET"))['Contents']
# return render_template("Index.html", objects=objects)
return render_template("Index.html", page_title="Home")
@app.route('/upload/')
@app.route("/upload/")
def Upload():
return render_template("Upload.html", page_title="Upload")
@app.route('/api/upload/', methods=['POST'])
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
def upload_file(file, bucket, key, mimetype) -> bool | Exception:
CHUNK_SIZE = 1024 * 1024 * 4
uploaded_parts = []
try:
__upload = s3.create_multipart_upload(
ACL="public-read",
Bucket=bucket,
Key=key,
ContentType=mimetype,
)
isFileRead = False
part_num = 1
while not isFileRead:
chunk = file.read(CHUNK_SIZE)
if len(chunk) == 0:
isFileRead = True
s3.complete_multipart_upload(
Bucket=bucket,
Key=key,
UploadId=__upload["UploadId"],
MultipartUpload={"Parts": uploaded_parts},
)
break
try:
part = s3.upload_part(
Bucket=bucket,
Key=key,
PartNumber=part_num,
ContentLength=len(chunk),
UploadId=__upload["UploadId"],
Body=chunk,
)
uploaded_parts.append({"ETag": part["ETag"], "PartNumber": part_num})
part_num += 1
except (ClientError, SSLError) as e:
s3.abort_multipart_upload(
Bucket=bucket,
Key=key,
UploadId=__upload["UploadId"],
)
return e
except (ClientError, SSLError) as e:
return e
return True
@app.route("/api/upload/", methods=["POST"])
def ApiUpload():
print(request.files['file'])
print(request.form)
tags = request.form.get("tags").split(",")
urls = []
for url in request.form.get("urls").split(";"):
if url == "":
continue
urls.append(json.loads(url))
file = request.files.get("file")
if file.filename == "":
return Response(
'{"status": "error", "message": "FLASK ERR: GET FILE ERROR"}', 500
)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
else:
return Response(
'{"status": "error", "message": "FLASK ERR: UNSUPPORTED EXTENSION"}', 500
)
Image = PIL.Image.open(os.path.join(app.config["UPLOAD_FOLDER"], filename))
TMP_FILE = open(os.path.join(app.config["UPLOAD_FOLDER"], filename), "rb")
file_path = filename.split(".")[0]
file_ext = filename.split(".")[-1]
s3OrigFileResponse = upload_file(
TMP_FILE, os.getenv("AWS_BUCKET"), f"{file_path}/{filename}", file.mimetype
)
if s3OrigFileResponse is not True:
return Response(
json.dumps({"status": "error", "message": f"S3 ERR: {s3OrigFileResponse}"}),
500,
)
try:
db.collection(os.getenv("PREFIX")).add(
{
"mimetype": file.mimetype,
"width": Image.width,
"height": Image.height,
"date": int(request.form.get("date")),
"alt": request.form.get("alt") or "",
"tags": tags or [],
"urls": urls or [],
},
request.files["file"].filename,
)
except exceptions.CONFLICT:
return Response(
'{"status": "error", "message": "FIRESTORE ERR: CONFLICT"}', 400
)
return {"status": "ok", "message": "Uploaded"}

View file

@ -1,5 +1,8 @@
python-dotenv
python-dotenv # Load environment variables from .env
Flask
boto3
pillow
boto3==1.35.99 # AWS SDK for Python (S3)
boto3-stubs[essential]
firebase-admin # Firebase Admin SDK for Python (FireStore)
Flask # Web framework
pillow # Image processing

View file

@ -56,9 +56,9 @@
</div>
</div>
<div class="w-full flex flex-wrap gap-2">
<button type="submit"
<button type="submit" id="btn-submit"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Upload</button>
<button type="reset"
<button type="reset" id="btn-reset"
class="focus:outline-none text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900">Reset</button>
</div>
</div>
@ -87,7 +87,7 @@
}
const tagSearch = ["Winter", "Summer", "Spring", "Autumn"];
const tags = [];
let tags = [];
const tagInp = document.getElementById('tags-input')
const tagCon = document.getElementById('tags-container')
@ -160,6 +160,22 @@
}
});
const btnReset = document.getElementById('btn-reset');
const btnSubmit = document.getElementById('btn-submit');
btnReset.addEventListener('click', () => {
imgCon.classList.add("hidden")
imgCon.classList.remove("flex")
imgPre.src = '';
tagInp.value = '';
tagCon.innerHTML = '';
tags = [];
urlInp.value = '';
urlCon.innerHTML = '';
urls = [];
datepicker.setDate("today");
});
const form = document.getElementById('upload-form');
form.addEventListener('keydown', (e) => {
if (e.key === "Enter") {
@ -168,6 +184,7 @@
});
form.addEventListener('submit', (e) => {
e.preventDefault();
btnSubmit.disabled = true;
const formFields = form.elements;
const formData = new FormData(form);
@ -205,10 +222,13 @@
.then(res => res.json())
.then(data => {
console.log(data);
btnSubmit.disabled = false;
alert(data.message);
window.location.href = "{{ url_for('Home') }}";
})
.catch(err => {
console.log(err);
btnSubmit.disabled = false;
})
});