Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ 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 |
|
@@ -12,7 +13,7 @@ logging.basicConfig(level=logging.DEBUG)
|
|
12 |
logger = app.logger
|
13 |
|
14 |
UPLOAD_FOLDER = '/tmp'
|
15 |
-
ALLOWED_EXTENSIONS = {'mp3', 'wav'}
|
16 |
|
17 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
18 |
|
@@ -41,17 +42,13 @@ def process_file():
|
|
41 |
|
42 |
factor = int(request.form['factor'])
|
43 |
is_decrypting = filename.lower().endswith('.mp3')
|
44 |
-
logger.info(f"Processing file: {'
|
45 |
|
46 |
try:
|
47 |
if is_decrypting:
|
48 |
-
output =
|
49 |
-
output_filename = 'deconverted_file.wav'
|
50 |
-
mimetype = 'audio/wav'
|
51 |
else:
|
52 |
-
output =
|
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)
|
@@ -71,39 +68,47 @@ def process_file():
|
|
71 |
logger.error("Invalid file type")
|
72 |
return jsonify({'error': 'Invalid file type'}), 400
|
73 |
|
74 |
-
def
|
75 |
-
logger.info(f"
|
76 |
-
with
|
77 |
-
|
78 |
-
frames = wav_file.readframes(wav_file.getnframes())
|
79 |
|
80 |
-
|
81 |
-
|
82 |
|
|
|
83 |
output = io.BytesIO()
|
84 |
with wave.open(output, 'wb') as wav_output:
|
85 |
-
wav_output.
|
86 |
-
wav_output.
|
|
|
|
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
def
|
92 |
-
logger.info(f"
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
94 |
params = wav_file.getparams()
|
95 |
frames = wav_file.readframes(wav_file.getnframes())
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
output = io.BytesIO()
|
101 |
-
with wave.open(output, 'wb') as wav_output:
|
102 |
-
wav_output.setparams(params)
|
103 |
-
wav_output.writeframes(struct.pack(f'{len(modified_data)}h', *modified_data))
|
104 |
|
105 |
-
logger.info(f"
|
106 |
-
return
|
107 |
|
108 |
if __name__ == '__main__':
|
109 |
app.run(debug=True)
|
|
|
5 |
import logging
|
6 |
from flask import Flask, render_template, request, send_file, jsonify
|
7 |
from werkzeug.utils import secure_filename
|
8 |
+
from pydub import AudioSegment
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
13 |
logger = app.logger
|
14 |
|
15 |
UPLOAD_FOLDER = '/tmp'
|
16 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp3', 'wav', 'txt', 'pdf'}
|
17 |
|
18 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
19 |
|
|
|
42 |
|
43 |
factor = int(request.form['factor'])
|
44 |
is_decrypting = filename.lower().endswith('.mp3')
|
45 |
+
logger.info(f"Processing file: {'decrypting' if is_decrypting else 'encrypting'} with factor {factor}")
|
46 |
|
47 |
try:
|
48 |
if is_decrypting:
|
49 |
+
output, output_filename, mimetype = decrypt_file(filepath, factor)
|
|
|
|
|
50 |
else:
|
51 |
+
output, output_filename, mimetype = encrypt_file(filepath, factor)
|
|
|
|
|
52 |
|
53 |
logger.info(f"File processed successfully. Output size: {len(output)} bytes")
|
54 |
os.remove(filepath)
|
|
|
68 |
logger.error("Invalid file type")
|
69 |
return jsonify({'error': 'Invalid file type'}), 400
|
70 |
|
71 |
+
def encrypt_file(filepath, factor):
|
72 |
+
logger.info(f"Encrypting file: {filepath}")
|
73 |
+
with open(filepath, 'rb') as file:
|
74 |
+
data = file.read()
|
|
|
75 |
|
76 |
+
# Convert data to audio samples
|
77 |
+
samples = [ord(byte) * factor for byte in data.decode('latin-1')]
|
78 |
|
79 |
+
# Create WAV file
|
80 |
output = io.BytesIO()
|
81 |
with wave.open(output, 'wb') as wav_output:
|
82 |
+
wav_output.setnchannels(1)
|
83 |
+
wav_output.setsampwidth(2)
|
84 |
+
wav_output.setframerate(44100)
|
85 |
+
wav_output.writeframes(struct.pack(f'{len(samples)}h', *samples))
|
86 |
|
87 |
+
# Convert WAV to MP3
|
88 |
+
output.seek(0)
|
89 |
+
audio = AudioSegment.from_wav(output)
|
90 |
+
mp3_output = io.BytesIO()
|
91 |
+
audio.export(mp3_output, format="mp3")
|
92 |
+
|
93 |
+
logger.info(f"Encryption complete. Output size: {mp3_output.getbuffer().nbytes} bytes")
|
94 |
+
return mp3_output.getvalue(), 'encrypted.mp3', 'audio/mpeg'
|
95 |
|
96 |
+
def decrypt_file(filepath, factor):
|
97 |
+
logger.info(f"Decrypting file: {filepath}")
|
98 |
+
audio = AudioSegment.from_mp3(filepath)
|
99 |
+
wav_data = io.BytesIO()
|
100 |
+
audio.export(wav_data, format="wav")
|
101 |
+
wav_data.seek(0)
|
102 |
+
|
103 |
+
with wave.open(wav_data, 'rb') as wav_file:
|
104 |
params = wav_file.getparams()
|
105 |
frames = wav_file.readframes(wav_file.getnframes())
|
106 |
|
107 |
+
samples = struct.unpack(f'{len(frames)//2}h', frames)
|
108 |
+
decrypted_data = ''.join(chr(int(sample / factor)) for sample in samples).encode('latin-1')
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
logger.info(f"Decryption complete. Output size: {len(decrypted_data)} bytes")
|
111 |
+
return decrypted_data, 'decrypted_file', 'application/octet-stream'
|
112 |
|
113 |
if __name__ == '__main__':
|
114 |
app.run(debug=True)
|