Spaces:
Running
Running
file cleanup
Browse files
app.py
CHANGED
|
@@ -34,6 +34,7 @@ import shutil
|
|
| 34 |
import urllib.parse
|
| 35 |
import mimetypes
|
| 36 |
import threading
|
|
|
|
| 37 |
|
| 38 |
# Gradio supported languages for syntax highlighting
|
| 39 |
GRADIO_SUPPORTED_LANGUAGES = [
|
|
@@ -1803,16 +1804,130 @@ def compress_audio_for_data_uri(audio_bytes: bytes, max_size_mb: int = 4) -> byt
|
|
| 1803 |
print(f"[AudioCompress] Compression failed: {e}, using original audio")
|
| 1804 |
return audio_bytes
|
| 1805 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1806 |
# Global dictionary to store temporary media files for the session
|
| 1807 |
temp_media_files = {}
|
| 1808 |
|
| 1809 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1810 |
"""Create a temporary file and return a local URL for preview.
|
| 1811 |
|
| 1812 |
Args:
|
| 1813 |
media_bytes: Raw bytes of the media file
|
| 1814 |
filename: Name for the file (will be made unique)
|
| 1815 |
media_type: Type of media ('image', 'video', 'audio')
|
|
|
|
| 1816 |
|
| 1817 |
Returns:
|
| 1818 |
Temporary file URL for preview or error message
|
|
@@ -1824,15 +1939,18 @@ def create_temp_media_url(media_bytes: bytes, filename: str, media_type: str = "
|
|
| 1824 |
base_name, ext = os.path.splitext(filename)
|
| 1825 |
unique_filename = f"{media_type}_{timestamp}_{unique_id}_{base_name}{ext}"
|
| 1826 |
|
| 1827 |
-
# Create temporary file in
|
| 1828 |
-
|
| 1829 |
-
os.
|
| 1830 |
-
temp_path = os.path.join(temp_dir, unique_filename)
|
| 1831 |
|
| 1832 |
# Write media bytes to temporary file
|
| 1833 |
with open(temp_path, 'wb') as f:
|
| 1834 |
f.write(media_bytes)
|
| 1835 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1836 |
# Store the file info for later upload
|
| 1837 |
file_id = f"{media_type}_{unique_id}"
|
| 1838 |
temp_media_files[file_id] = {
|
|
@@ -4146,8 +4264,10 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
| 4146 |
try:
|
| 4147 |
cleanup_session_videos(session_id)
|
| 4148 |
cleanup_session_audio(session_id)
|
|
|
|
| 4149 |
reap_old_videos()
|
| 4150 |
reap_old_audio()
|
|
|
|
| 4151 |
except Exception:
|
| 4152 |
pass
|
| 4153 |
|
|
@@ -7459,6 +7579,9 @@ with gr.Blocks(
|
|
| 7459 |
# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
|
| 7460 |
|
| 7461 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 7462 |
demo.queue(api_open=False, default_concurrency_limit=20).launch(
|
| 7463 |
show_api=False,
|
| 7464 |
ssr_mode=True,
|
|
|
|
| 34 |
import urllib.parse
|
| 35 |
import mimetypes
|
| 36 |
import threading
|
| 37 |
+
import atexit
|
| 38 |
|
| 39 |
# Gradio supported languages for syntax highlighting
|
| 40 |
GRADIO_SUPPORTED_LANGUAGES = [
|
|
|
|
| 1804 |
print(f"[AudioCompress] Compression failed: {e}, using original audio")
|
| 1805 |
return audio_bytes
|
| 1806 |
|
| 1807 |
+
# ---------------------------------------------------------------------------
|
| 1808 |
+
# General temp media file management (per-session tracking and cleanup)
|
| 1809 |
+
# ---------------------------------------------------------------------------
|
| 1810 |
+
MEDIA_TEMP_DIR = os.path.join(tempfile.gettempdir(), "anycoder_media")
|
| 1811 |
+
MEDIA_FILE_TTL_SECONDS = 6 * 60 * 60 # 6 hours
|
| 1812 |
+
_SESSION_MEDIA_FILES: Dict[str, List[str]] = {}
|
| 1813 |
+
_MEDIA_FILES_LOCK = threading.Lock()
|
| 1814 |
+
|
| 1815 |
# Global dictionary to store temporary media files for the session
|
| 1816 |
temp_media_files = {}
|
| 1817 |
|
| 1818 |
+
def _ensure_media_dir_exists() -> None:
|
| 1819 |
+
"""Ensure the media temp directory exists."""
|
| 1820 |
+
try:
|
| 1821 |
+
os.makedirs(MEDIA_TEMP_DIR, exist_ok=True)
|
| 1822 |
+
except Exception:
|
| 1823 |
+
pass
|
| 1824 |
+
|
| 1825 |
+
def track_session_media_file(session_id: Optional[str], file_path: str) -> None:
|
| 1826 |
+
"""Track a media file for session-based cleanup."""
|
| 1827 |
+
if not session_id or not file_path:
|
| 1828 |
+
return
|
| 1829 |
+
with _MEDIA_FILES_LOCK:
|
| 1830 |
+
if session_id not in _SESSION_MEDIA_FILES:
|
| 1831 |
+
_SESSION_MEDIA_FILES[session_id] = []
|
| 1832 |
+
_SESSION_MEDIA_FILES[session_id].append(file_path)
|
| 1833 |
+
|
| 1834 |
+
def cleanup_session_media(session_id: Optional[str]) -> None:
|
| 1835 |
+
"""Clean up media files for a specific session."""
|
| 1836 |
+
if not session_id:
|
| 1837 |
+
return
|
| 1838 |
+
with _MEDIA_FILES_LOCK:
|
| 1839 |
+
files_to_clean = _SESSION_MEDIA_FILES.pop(session_id, [])
|
| 1840 |
+
|
| 1841 |
+
for path in files_to_clean:
|
| 1842 |
+
try:
|
| 1843 |
+
if path and os.path.exists(path):
|
| 1844 |
+
os.unlink(path)
|
| 1845 |
+
except Exception:
|
| 1846 |
+
# Best-effort cleanup
|
| 1847 |
+
pass
|
| 1848 |
+
|
| 1849 |
+
def reap_old_media(ttl_seconds: int = MEDIA_FILE_TTL_SECONDS) -> None:
|
| 1850 |
+
"""Delete old media files in the temp directory based on modification time."""
|
| 1851 |
+
try:
|
| 1852 |
+
_ensure_media_dir_exists()
|
| 1853 |
+
now_ts = time.time()
|
| 1854 |
+
for name in os.listdir(MEDIA_TEMP_DIR):
|
| 1855 |
+
path = os.path.join(MEDIA_TEMP_DIR, name)
|
| 1856 |
+
if os.path.isfile(path):
|
| 1857 |
+
try:
|
| 1858 |
+
mtime = os.path.getmtime(path)
|
| 1859 |
+
if (now_ts - mtime) > ttl_seconds:
|
| 1860 |
+
os.unlink(path)
|
| 1861 |
+
except Exception:
|
| 1862 |
+
pass
|
| 1863 |
+
except Exception:
|
| 1864 |
+
# Temp dir might not exist or be accessible; ignore
|
| 1865 |
+
pass
|
| 1866 |
+
|
| 1867 |
+
def cleanup_all_temp_media_on_startup() -> None:
|
| 1868 |
+
"""Clean up all temporary media files on app startup."""
|
| 1869 |
+
try:
|
| 1870 |
+
# Clean up temp_media_files registry
|
| 1871 |
+
temp_media_files.clear()
|
| 1872 |
+
|
| 1873 |
+
# Clean up actual files from disk (assume all are orphaned on startup)
|
| 1874 |
+
_ensure_media_dir_exists()
|
| 1875 |
+
for name in os.listdir(MEDIA_TEMP_DIR):
|
| 1876 |
+
path = os.path.join(MEDIA_TEMP_DIR, name)
|
| 1877 |
+
if os.path.isfile(path):
|
| 1878 |
+
try:
|
| 1879 |
+
os.unlink(path)
|
| 1880 |
+
except Exception:
|
| 1881 |
+
pass
|
| 1882 |
+
|
| 1883 |
+
# Clear session tracking
|
| 1884 |
+
with _MEDIA_FILES_LOCK:
|
| 1885 |
+
_SESSION_MEDIA_FILES.clear()
|
| 1886 |
+
|
| 1887 |
+
print("[StartupCleanup] Cleaned up orphaned temporary media files")
|
| 1888 |
+
except Exception as e:
|
| 1889 |
+
print(f"[StartupCleanup] Error during media cleanup: {str(e)}")
|
| 1890 |
+
|
| 1891 |
+
def cleanup_all_temp_media_on_shutdown() -> None:
|
| 1892 |
+
"""Clean up all temporary media files on app shutdown."""
|
| 1893 |
+
try:
|
| 1894 |
+
print("[ShutdownCleanup] Cleaning up temporary media files...")
|
| 1895 |
+
|
| 1896 |
+
# Clean up temp_media_files registry and remove files
|
| 1897 |
+
for file_id, file_info in temp_media_files.items():
|
| 1898 |
+
try:
|
| 1899 |
+
if os.path.exists(file_info['path']):
|
| 1900 |
+
os.unlink(file_info['path'])
|
| 1901 |
+
except Exception:
|
| 1902 |
+
pass
|
| 1903 |
+
temp_media_files.clear()
|
| 1904 |
+
|
| 1905 |
+
# Clean up all session files
|
| 1906 |
+
with _MEDIA_FILES_LOCK:
|
| 1907 |
+
for session_id, file_paths in _SESSION_MEDIA_FILES.items():
|
| 1908 |
+
for path in file_paths:
|
| 1909 |
+
try:
|
| 1910 |
+
if path and os.path.exists(path):
|
| 1911 |
+
os.unlink(path)
|
| 1912 |
+
except Exception:
|
| 1913 |
+
pass
|
| 1914 |
+
_SESSION_MEDIA_FILES.clear()
|
| 1915 |
+
|
| 1916 |
+
print("[ShutdownCleanup] Temporary media cleanup completed")
|
| 1917 |
+
except Exception as e:
|
| 1918 |
+
print(f"[ShutdownCleanup] Error during cleanup: {str(e)}")
|
| 1919 |
+
|
| 1920 |
+
# Register shutdown cleanup handler
|
| 1921 |
+
atexit.register(cleanup_all_temp_media_on_shutdown)
|
| 1922 |
+
|
| 1923 |
+
def create_temp_media_url(media_bytes: bytes, filename: str, media_type: str = "image", session_id: Optional[str] = None) -> str:
|
| 1924 |
"""Create a temporary file and return a local URL for preview.
|
| 1925 |
|
| 1926 |
Args:
|
| 1927 |
media_bytes: Raw bytes of the media file
|
| 1928 |
filename: Name for the file (will be made unique)
|
| 1929 |
media_type: Type of media ('image', 'video', 'audio')
|
| 1930 |
+
session_id: Session ID for tracking cleanup
|
| 1931 |
|
| 1932 |
Returns:
|
| 1933 |
Temporary file URL for preview or error message
|
|
|
|
| 1939 |
base_name, ext = os.path.splitext(filename)
|
| 1940 |
unique_filename = f"{media_type}_{timestamp}_{unique_id}_{base_name}{ext}"
|
| 1941 |
|
| 1942 |
+
# Create temporary file in the dedicated directory
|
| 1943 |
+
_ensure_media_dir_exists()
|
| 1944 |
+
temp_path = os.path.join(MEDIA_TEMP_DIR, unique_filename)
|
|
|
|
| 1945 |
|
| 1946 |
# Write media bytes to temporary file
|
| 1947 |
with open(temp_path, 'wb') as f:
|
| 1948 |
f.write(media_bytes)
|
| 1949 |
|
| 1950 |
+
# Track file for session-based cleanup
|
| 1951 |
+
if session_id:
|
| 1952 |
+
track_session_media_file(session_id, temp_path)
|
| 1953 |
+
|
| 1954 |
# Store the file info for later upload
|
| 1955 |
file_id = f"{media_type}_{unique_id}"
|
| 1956 |
temp_media_files[file_id] = {
|
|
|
|
| 4264 |
try:
|
| 4265 |
cleanup_session_videos(session_id)
|
| 4266 |
cleanup_session_audio(session_id)
|
| 4267 |
+
cleanup_session_media(session_id)
|
| 4268 |
reap_old_videos()
|
| 4269 |
reap_old_audio()
|
| 4270 |
+
reap_old_media()
|
| 4271 |
except Exception:
|
| 4272 |
pass
|
| 4273 |
|
|
|
|
| 7579 |
# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
|
| 7580 |
|
| 7581 |
if __name__ == "__main__":
|
| 7582 |
+
# Clean up any orphaned temporary files from previous runs
|
| 7583 |
+
cleanup_all_temp_media_on_startup()
|
| 7584 |
+
|
| 7585 |
demo.queue(api_open=False, default_concurrency_limit=20).launch(
|
| 7586 |
show_api=False,
|
| 7587 |
ssr_mode=True,
|