Sergidev commited on
Commit
fa163a1
·
verified ·
1 Parent(s): 3ffb9df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -11
app.py CHANGED
@@ -5,6 +5,8 @@ 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
 
@@ -40,7 +42,7 @@ def process_file():
40
  logger.info(f"File saved: {filepath}")
41
 
42
  factor = int(request.form['factor'])
43
- is_decrypting = filename.lower().endswith('.wav')
44
  logger.info(f"Processing file: {'decrypting' if is_decrypting else 'encrypting'} with factor {factor}")
45
 
46
  try:
@@ -76,27 +78,45 @@ def encrypt_file(filepath, factor):
76
  samples = [ord(byte) * factor for byte in data.decode('latin-1')]
77
 
78
  # Create WAV file
79
- output = io.BytesIO()
80
- with wave.open(output, 'wb') as wav_output:
81
- wav_output.setnchannels(1)
82
- wav_output.setsampwidth(2)
83
- wav_output.setframerate(44100)
84
- wav_output.writeframes(struct.pack(f'{len(samples)}h', *samples))
85
 
86
- logger.info(f"Encryption complete. Output size: {output.getbuffer().nbytes} bytes")
87
- return output.getvalue(), 'encrypted.wav', 'audio/wav'
 
 
 
 
 
 
88
 
89
  def decrypt_file(filepath, factor):
90
  logger.info(f"Decrypting file: {filepath}")
91
- with wave.open(filepath, 'rb') as wav_file:
 
 
 
 
 
 
 
92
  params = wav_file.getparams()
93
  frames = wav_file.readframes(wav_file.getnframes())
94
 
95
  samples = struct.unpack(f'{len(frames)//2}h', frames)
96
  decrypted_data = ''.join(chr(int(sample / factor)) for sample in samples).encode('latin-1')
97
 
 
 
 
 
 
98
  logger.info(f"Decryption complete. Output size: {len(decrypted_data)} bytes")
99
- return decrypted_data, 'decrypted_file', 'application/octet-stream'
100
 
101
  if __name__ == '__main__':
102
  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
+ import magic
10
 
11
  app = Flask(__name__)
12
 
 
42
  logger.info(f"File saved: {filepath}")
43
 
44
  factor = int(request.form['factor'])
45
+ is_decrypting = filename.lower().endswith('.mp3')
46
  logger.info(f"Processing file: {'decrypting' if is_decrypting else 'encrypting'} with factor {factor}")
47
 
48
  try:
 
78
  samples = [ord(byte) * factor for byte in data.decode('latin-1')]
79
 
80
  # Create WAV file
81
+ wav_output = io.BytesIO()
82
+ with wave.open(wav_output, 'wb') as wav_file:
83
+ wav_file.setnchannels(1)
84
+ wav_file.setsampwidth(2)
85
+ wav_file.setframerate(44100)
86
+ wav_file.writeframes(struct.pack(f'{len(samples)}h', *samples))
87
 
88
+ # Convert WAV to MP3
89
+ wav_output.seek(0)
90
+ audio = AudioSegment.from_wav(wav_output)
91
+ mp3_output = io.BytesIO()
92
+ audio.export(mp3_output, format="mp3")
93
+
94
+ logger.info(f"Encryption complete. Output size: {mp3_output.getbuffer().nbytes} bytes")
95
+ return mp3_output.getvalue(), 'encrypted.mp3', 'audio/mpeg'
96
 
97
  def decrypt_file(filepath, factor):
98
  logger.info(f"Decrypting file: {filepath}")
99
+
100
+ # Convert MP3 to WAV
101
+ audio = AudioSegment.from_mp3(filepath)
102
+ wav_output = io.BytesIO()
103
+ audio.export(wav_output, format="wav")
104
+ wav_output.seek(0)
105
+
106
+ with wave.open(wav_output, 'rb') as wav_file:
107
  params = wav_file.getparams()
108
  frames = wav_file.readframes(wav_file.getnframes())
109
 
110
  samples = struct.unpack(f'{len(frames)//2}h', frames)
111
  decrypted_data = ''.join(chr(int(sample / factor)) for sample in samples).encode('latin-1')
112
 
113
+ # Detect original file type
114
+ mime = magic.Magic(mime=True)
115
+ file_type = mime.from_buffer(decrypted_data)
116
+ file_extension = file_type.split('/')[-1]
117
+
118
  logger.info(f"Decryption complete. Output size: {len(decrypted_data)} bytes")
119
+ return decrypted_data, f'decrypted_file.{file_extension}', file_type
120
 
121
  if __name__ == '__main__':
122
  app.run(debug=True)