Skip to content

Commit 6631dca

Browse files
committed
cloudinary
1 parent 49e66c3 commit 6631dca

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

app.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from dotenv import load_dotenv
2+
load_dotenv() # reads .env into environment
3+
14
import os
25
import math
36

@@ -10,6 +13,12 @@
1013
app = Flask(__name__)
1114
app.config["SECRET_KEY"] = "secret"
1215

16+
# cloudinary.config(
17+
# cloud_name = os.getenv("CLOUDINARY_CLOUD_NAME"),
18+
# api_key = os.getenv("CLOUDINARY_API_KEY"),
19+
# api_secret = os.getenv("CLOUDINARY_API_SECRET")
20+
# )
21+
1322

1423
@app.get("/")
1524
def home():
@@ -123,8 +132,24 @@ def generate_cadastral_plan():
123132

124133
# drawer.save_dxf()
125134
# drawer.dxf_to_dwg()
126-
drawer.save()
127-
return jsonify({"message": "Cadastral plan generated", "filename": plan.name}), 200
135+
url = drawer.save()
136+
return jsonify({"message": "Cadastral plan generated", "filename": plan.name, "url": url}), 200
137+
138+
@app.errorhandler(404)
139+
def not_found(e):
140+
return jsonify({"error": "Resource not found"}), 404
141+
142+
@app.errorhandler(500)
143+
def internal_error(e):
144+
return jsonify({"error": "Something went wrong on our side"}), 500
145+
146+
@app.errorhandler(Exception)
147+
def handle_exception(e):
148+
# You can log the exception here
149+
app.logger.error(f"Unhandled Exception: {e}", exc_info=True)
150+
151+
# Return JSON response instead of crashing
152+
return jsonify({"error": "An unexpected error occurred"}), 500
128153

129154
if __name__ == '__main__':
130155
port = int(os.environ.get("PORT", 8080))

dxf.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import uuid
1111
import zipfile
1212
import shutil
13+
from upload import upload_file
1314

1415
class SurveyDXFManager:
1516
def __init__(self, plan_name: str = "Survey Plan", scale: float = 1.0):
@@ -216,9 +217,10 @@ def save(self):
216217
zipf.write(dwg_path, os.path.basename(dwg_path))
217218
zipf.write(pdf_path, os.path.basename(pdf_path))
218219

219-
220-
# Copy DWG to a permanent location
221-
shutil.copy(zip_path, f"{filename}.zip")
220+
url = upload_file(zip_path, folder="survey_plans")
221+
if url is None:
222+
raise Exception("Upload failed")
223+
return url
222224

223225

224226

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ annotated-types==0.7.0
22
beautifulsoup4==4.13.5
33
blinker==1.9.0
44
bs4==0.0.2
5+
certifi==2025.8.3
56
click==8.2.1
7+
cloudinary==1.44.1
8+
dotenv==0.9.9
69
ezdxf==1.4.2
710
Flask==3.1.2
811
fonttools==4.59.2
@@ -17,7 +20,10 @@ pydantic==2.11.9
1720
pydantic_core==2.33.2
1821
PyMuPDF==1.26.4
1922
pyparsing==3.2.4
23+
python-dotenv==1.1.1
24+
six==1.17.0
2025
soupsieve==2.8
2126
typing-inspection==0.4.1
2227
typing_extensions==4.15.0
28+
urllib3==2.5.0
2329
Werkzeug==3.1.3

upload.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
import cloudinary
4+
import cloudinary.uploader
5+
import cloudinary.api
6+
7+
def upload_file(file_path, folder="uploads"):
8+
"""
9+
Uploads a file to Cloudinary and returns the upload response.
10+
"""
11+
print(os.getenv("CLOUDINARY_URL"))
12+
try:
13+
response = cloudinary.uploader.upload(
14+
file_path,
15+
folder=folder, # optional: organizes uploads in a folder
16+
resource_type="auto" # auto-detect (image, raw, video, etc.)
17+
)
18+
return response.get("secure_url")
19+
except Exception as e:
20+
print(f"Upload failed: {e}")
21+
return None

0 commit comments

Comments
 (0)