Spaces:
Sleeping
Sleeping
fix: use /tmp directory for file storage and serving
Browse files
app.py
CHANGED
@@ -12,14 +12,13 @@ logging.basicConfig(level=logging.DEBUG)
|
|
12 |
app = Flask(__name__)
|
13 |
app.config['DEBUG'] = True # This enables debug mode for Flask
|
14 |
|
15 |
-
# Configure upload folders with absolute paths
|
16 |
-
|
17 |
-
|
18 |
-
THUMBNAIL_FOLDER = os.path.join(BASE_DIR, 'static', 'thumbnails')
|
19 |
|
20 |
-
# Ensure directories exist
|
21 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
22 |
-
os.makedirs(THUMBNAIL_FOLDER, exist_ok=True)
|
23 |
|
24 |
def get_watermark_position(img_width, img_height, wm_width, wm_height, position):
|
25 |
if position == "top-left":
|
@@ -160,12 +159,12 @@ def upload():
|
|
160 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
161 |
with open(filepath, 'wb') as f:
|
162 |
f.write(watermarked_file.getbuffer())
|
163 |
-
watermarked_file_urls.append(url_for('
|
164 |
watermarked_file_paths.append(filepath)
|
165 |
|
166 |
thumbnail_path = os.path.join(THUMBNAIL_FOLDER, filename)
|
167 |
create_thumbnail(filepath, thumbnail_path)
|
168 |
-
thumbnail_urls.append(url_for('
|
169 |
|
170 |
zip_filename = "watermarked_images.zip"
|
171 |
zip_filepath = os.path.join(UPLOAD_FOLDER, zip_filename)
|
@@ -173,11 +172,24 @@ def upload():
|
|
173 |
for file_path in watermarked_file_paths:
|
174 |
zipf.write(file_path, os.path.basename(file_path))
|
175 |
|
176 |
-
zip_url = url_for('
|
177 |
|
178 |
return render_template('results.html', thumbnail_urls=thumbnail_urls,
|
179 |
watermarked_file_urls=watermarked_file_urls, zip_url=zip_url)
|
180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
if __name__ == '__main__':
|
182 |
# Use the port that Hugging Face Spaces expects
|
183 |
port = int(os.environ.get('PORT', 7860))
|
|
|
12 |
app = Flask(__name__)
|
13 |
app.config['DEBUG'] = True # This enables debug mode for Flask
|
14 |
|
15 |
+
# Configure upload folders with absolute paths in /tmp
|
16 |
+
UPLOAD_FOLDER = '/tmp/watermarked_images'
|
17 |
+
THUMBNAIL_FOLDER = '/tmp/thumbnails'
|
|
|
18 |
|
19 |
+
# Ensure directories exist with proper permissions
|
20 |
+
os.makedirs(UPLOAD_FOLDER, mode=0o777, exist_ok=True)
|
21 |
+
os.makedirs(THUMBNAIL_FOLDER, mode=0o777, exist_ok=True)
|
22 |
|
23 |
def get_watermark_position(img_width, img_height, wm_width, wm_height, position):
|
24 |
if position == "top-left":
|
|
|
159 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
160 |
with open(filepath, 'wb') as f:
|
161 |
f.write(watermarked_file.getbuffer())
|
162 |
+
watermarked_file_urls.append(url_for('serve_watermarked', filename=filename))
|
163 |
watermarked_file_paths.append(filepath)
|
164 |
|
165 |
thumbnail_path = os.path.join(THUMBNAIL_FOLDER, filename)
|
166 |
create_thumbnail(filepath, thumbnail_path)
|
167 |
+
thumbnail_urls.append(url_for('serve_thumbnail', filename=filename))
|
168 |
|
169 |
zip_filename = "watermarked_images.zip"
|
170 |
zip_filepath = os.path.join(UPLOAD_FOLDER, zip_filename)
|
|
|
172 |
for file_path in watermarked_file_paths:
|
173 |
zipf.write(file_path, os.path.basename(file_path))
|
174 |
|
175 |
+
zip_url = url_for('download_file', filename=zip_filename)
|
176 |
|
177 |
return render_template('results.html', thumbnail_urls=thumbnail_urls,
|
178 |
watermarked_file_urls=watermarked_file_urls, zip_url=zip_url)
|
179 |
|
180 |
+
@app.route('/watermarked/<filename>')
|
181 |
+
def serve_watermarked(filename):
|
182 |
+
return send_file(os.path.join(UPLOAD_FOLDER, filename))
|
183 |
+
|
184 |
+
@app.route('/thumbnail/<filename>')
|
185 |
+
def serve_thumbnail(filename):
|
186 |
+
return send_file(os.path.join(THUMBNAIL_FOLDER, filename))
|
187 |
+
|
188 |
+
@app.route('/download/<filename>')
|
189 |
+
def download_file(filename):
|
190 |
+
return send_file(os.path.join(UPLOAD_FOLDER, filename),
|
191 |
+
as_attachment=True)
|
192 |
+
|
193 |
if __name__ == '__main__':
|
194 |
# Use the port that Hugging Face Spaces expects
|
195 |
port = int(os.environ.get('PORT', 7860))
|