Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,15 @@ import os
|
|
2 |
import io
|
3 |
import wave
|
4 |
import struct
|
|
|
5 |
from flask import Flask, render_template, request, send_file, jsonify
|
6 |
from werkzeug.utils import secure_filename
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
|
|
|
|
|
|
10 |
UPLOAD_FOLDER = '/tmp'
|
11 |
ALLOWED_EXTENSIONS = {'mp3', 'wav'}
|
12 |
|
@@ -21,18 +25,23 @@ def index():
|
|
21 |
|
22 |
@app.route('/process', methods=['POST'])
|
23 |
def process_file():
|
|
|
24 |
if 'file' not in request.files:
|
|
|
25 |
return jsonify({'error': 'No file part'}), 400
|
26 |
file = request.files['file']
|
27 |
if file.filename == '':
|
|
|
28 |
return jsonify({'error': 'No selected file'}), 400
|
29 |
if file and allowed_file(file.filename):
|
30 |
filename = secure_filename(file.filename)
|
31 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
32 |
file.save(filepath)
|
|
|
33 |
|
34 |
factor = int(request.form['factor'])
|
35 |
is_decrypting = filename.lower().endswith('.mp3')
|
|
|
36 |
|
37 |
try:
|
38 |
if is_decrypting:
|
@@ -44,7 +53,9 @@ def process_file():
|
|
44 |
output_filename = 'converted_file.mp3'
|
45 |
mimetype = 'audio/mpeg'
|
46 |
|
|
|
47 |
os.remove(filepath)
|
|
|
48 |
|
49 |
return send_file(
|
50 |
io.BytesIO(output),
|
@@ -53,12 +64,15 @@ def process_file():
|
|
53 |
download_name=output_filename
|
54 |
)
|
55 |
except Exception as e:
|
|
|
56 |
os.remove(filepath)
|
57 |
return jsonify({'error': str(e)}), 500
|
58 |
|
|
|
59 |
return jsonify({'error': 'Invalid file type'}), 400
|
60 |
|
61 |
def convert_file(filepath, factor):
|
|
|
62 |
with wave.open(filepath, 'rb') as wav_file:
|
63 |
params = wav_file.getparams()
|
64 |
frames = wav_file.readframes(wav_file.getnframes())
|
@@ -71,9 +85,11 @@ def convert_file(filepath, factor):
|
|
71 |
wav_output.setparams(params)
|
72 |
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
73 |
|
|
|
74 |
return output.getvalue()
|
75 |
|
76 |
def deconvert_file(filepath, factor):
|
|
|
77 |
with wave.open(filepath, 'rb') as wav_file:
|
78 |
params = wav_file.getparams()
|
79 |
frames = wav_file.readframes(wav_file.getnframes())
|
@@ -86,6 +102,7 @@ def deconvert_file(filepath, factor):
|
|
86 |
wav_output.setparams(params)
|
87 |
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
88 |
|
|
|
89 |
return output.getvalue()
|
90 |
|
91 |
if __name__ == '__main__':
|
|
|
2 |
import io
|
3 |
import wave
|
4 |
import struct
|
5 |
+
import logging
|
6 |
from flask import Flask, render_template, request, send_file, jsonify
|
7 |
from werkzeug.utils import secure_filename
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
+
logging.basicConfig(level=logging.DEBUG)
|
12 |
+
logger = app.logger
|
13 |
+
|
14 |
UPLOAD_FOLDER = '/tmp'
|
15 |
ALLOWED_EXTENSIONS = {'mp3', 'wav'}
|
16 |
|
|
|
25 |
|
26 |
@app.route('/process', methods=['POST'])
|
27 |
def process_file():
|
28 |
+
logger.info("Processing file request received")
|
29 |
if 'file' not in request.files:
|
30 |
+
logger.error("No file part in the request")
|
31 |
return jsonify({'error': 'No file part'}), 400
|
32 |
file = request.files['file']
|
33 |
if file.filename == '':
|
34 |
+
logger.error("No selected file")
|
35 |
return jsonify({'error': 'No selected file'}), 400
|
36 |
if file and allowed_file(file.filename):
|
37 |
filename = secure_filename(file.filename)
|
38 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
39 |
file.save(filepath)
|
40 |
+
logger.info(f"File saved: {filepath}")
|
41 |
|
42 |
factor = int(request.form['factor'])
|
43 |
is_decrypting = filename.lower().endswith('.mp3')
|
44 |
+
logger.info(f"Processing file: {'deconverting' if is_decrypting else 'converting'} with factor {factor}")
|
45 |
|
46 |
try:
|
47 |
if is_decrypting:
|
|
|
53 |
output_filename = 'converted_file.mp3'
|
54 |
mimetype = 'audio/mpeg'
|
55 |
|
56 |
+
logger.info(f"File processed successfully. Output size: {len(output)} bytes")
|
57 |
os.remove(filepath)
|
58 |
+
logger.info(f"Temporary file removed: {filepath}")
|
59 |
|
60 |
return send_file(
|
61 |
io.BytesIO(output),
|
|
|
64 |
download_name=output_filename
|
65 |
)
|
66 |
except Exception as e:
|
67 |
+
logger.error(f"Error processing file: {str(e)}")
|
68 |
os.remove(filepath)
|
69 |
return jsonify({'error': str(e)}), 500
|
70 |
|
71 |
+
logger.error("Invalid file type")
|
72 |
return jsonify({'error': 'Invalid file type'}), 400
|
73 |
|
74 |
def convert_file(filepath, factor):
|
75 |
+
logger.info(f"Converting file: {filepath}")
|
76 |
with wave.open(filepath, 'rb') as wav_file:
|
77 |
params = wav_file.getparams()
|
78 |
frames = wav_file.readframes(wav_file.getnframes())
|
|
|
85 |
wav_output.setparams(params)
|
86 |
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
87 |
|
88 |
+
logger.info(f"Conversion complete. Output size: {output.getbuffer().nbytes} bytes")
|
89 |
return output.getvalue()
|
90 |
|
91 |
def deconvert_file(filepath, factor):
|
92 |
+
logger.info(f"Deconverting file: {filepath}")
|
93 |
with wave.open(filepath, 'rb') as wav_file:
|
94 |
params = wav_file.getparams()
|
95 |
frames = wav_file.readframes(wav_file.getnframes())
|
|
|
102 |
wav_output.setparams(params)
|
103 |
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
104 |
|
105 |
+
logger.info(f"Deconversion complete. Output size: {output.getbuffer().nbytes} bytes")
|
106 |
return output.getvalue()
|
107 |
|
108 |
if __name__ == '__main__':
|