Adityadn commited on
Commit
00111e4
·
verified ·
1 Parent(s): 10d310f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -6,28 +6,49 @@ import ffmpeg
6
  # Atur tema Gradio
7
  gr.themes.Soft()
8
 
 
 
 
 
9
  # Format gambar yang didukung
10
  supported_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
11
  'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
12
  'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
13
  'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF']
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def convert_image_ffmpeg(image, target_format):
16
  try:
17
- # Ambil nama file asli tanpa ekstensi
18
- base_name = os.path.splitext(os.path.basename(image))[0]
 
 
 
 
 
19
 
20
  # Nama file keluaran dengan awalan yang diinginkan
21
  output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"
22
-
 
 
 
23
  # Gunakan ffmpeg untuk konversi
24
- (
25
- ffmpeg
26
- .input(image)
27
- .output(output_file, format=target_format)
28
- .overwrite_output()
29
- .run(capture_stdout=True, capture_stderr=True)
30
- )
31
 
32
  return output_file
33
  except Exception as e:
 
6
  # Atur tema Gradio
7
  gr.themes.Soft()
8
 
9
+ def delete_temp_dir(directory, delay=900): # 15 minutes = 900 seconds
10
+ timer = threading.Timer(delay, shutil.rmtree, [directory])
11
+ timer.start()
12
+
13
  # Format gambar yang didukung
14
  supported_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
15
  'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
16
  'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
17
  'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF']
18
 
19
+ CACHE_DIR = tempfile.mkdtemp()
20
+ delete_temp_dir(CACHE_DIR, delay=900) # Clean up cache after 15 minutes
21
+
22
+ def sanitize_filename(filename):
23
+ """Sanitize filename by removing special characters and spaces."""
24
+ return re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
25
+
26
+ def clean_cache():
27
+ """Remove files in the cache directory older than 15 minutes."""
28
+ now = time.time()
29
+ for file in os.listdir(CACHE_DIR):
30
+ file_path = os.path.join(CACHE_DIR, file)
31
+ if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900: # 15 minutes
32
+ os.remove(file_path)
33
+
34
  def convert_image_ffmpeg(image, target_format):
35
  try:
36
+ # Create a temporary directory for the uploaded file
37
+ temp_dir = tempfile.mkdtemp()
38
+
39
+ # Sanitize the filename and save the uploaded video to the temp directory
40
+ base_name = os.path.splitext(os.path.basename(image.name))[0]
41
+ sanitized_base_name = sanitize_filename(base_name)
42
+ image_path = os.path.join(temp_dir, f"{sanitized_base_name}.png") # Saving as mp4 by default for now
43
 
44
  # Nama file keluaran dengan awalan yang diinginkan
45
  output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"
46
+
47
+ with open(image_path, "wb") as f:
48
+ f.write(image.getbuffer()) # Save the uploaded video to a local file
49
+
50
  # Gunakan ffmpeg untuk konversi
51
+ ffmpeg.input(image_path).output(output_file, format=target_format).overwrite_output().run(capture_stdout=True, capture_stderr=True)
 
 
 
 
 
 
52
 
53
  return output_file
54
  except Exception as e: