From 8e99c3ca8df982ca40fa59f23a0488abdda6041b Mon Sep 17 00:00:00 2001 From: oiupoyt Date: Sun, 13 Sep 2026 11:35:51 +0530 Subject: [PATCH] feat(backend): remake backend in Rust with Axum, stream piping, and low memory footprint --- .gitignore | 3 + README.md | 29 +- {backend => backend-py}/main.py | 0 {backend => backend-py}/requirements.txt | 0 {backend => backend-py}/routers/info.py | 0 {backend => backend-py}/routers/instagram.py | 0 {backend => backend-py}/routers/youtube.py | 0 .../services/instagram_service.py | 0 .../services/youtube_service.py | 0 {backend => backend-py}/utils.py | 0 backend/Cargo.lock | 2000 +++++++++++++++++ backend/Cargo.toml | 29 + backend/Makefile | 16 + backend/src/config.rs | 62 + backend/src/error.rs | 60 + backend/src/main.rs | 161 ++ backend/src/routes/info.rs | 92 + backend/src/routes/instagram.rs | 123 + backend/src/routes/mod.rs | 47 + backend/src/routes/youtube.rs | 166 ++ backend/src/services/instagram.rs | 210 ++ backend/src/services/mod.rs | 3 + backend/src/services/youtube.rs | 336 +++ backend/src/services/ytdlp.rs | 125 ++ backend/src/utils.rs | 144 ++ 25 files changed, 3603 insertions(+), 3 deletions(-) rename {backend => backend-py}/main.py (100%) rename {backend => backend-py}/requirements.txt (100%) rename {backend => backend-py}/routers/info.py (100%) rename {backend => backend-py}/routers/instagram.py (100%) rename {backend => backend-py}/routers/youtube.py (100%) rename {backend => backend-py}/services/instagram_service.py (100%) rename {backend => backend-py}/services/youtube_service.py (100%) rename {backend => backend-py}/utils.py (100%) create mode 100644 backend/Cargo.lock create mode 100644 backend/Cargo.toml create mode 100644 backend/Makefile create mode 100644 backend/src/config.rs create mode 100644 backend/src/error.rs create mode 100644 backend/src/main.rs create mode 100644 backend/src/routes/info.rs create mode 100644 backend/src/routes/instagram.rs create mode 100644 backend/src/routes/mod.rs create mode 100644 backend/src/routes/youtube.rs create mode 100644 backend/src/services/instagram.rs create mode 100644 backend/src/services/mod.rs create mode 100644 backend/src/services/youtube.rs create mode 100644 backend/src/services/ytdlp.rs create mode 100644 backend/src/utils.rs diff --git a/.gitignore b/.gitignore index 0471669..64d1645 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ venv/ ENV/ *.log .DS_Store +target/ +downloads/ +yt-dlp diff --git a/README.md b/README.md index 8045111..81f5c73 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,33 @@ yeah ## features -- youtube downloads (video, audio, thumbnail, subtitles) -- instagram downloads (reels, posts) +- youtube downloads (video, audio, thumbnail, subtitles, channel art) +- instagram downloads (reels, posts, profile avatars, thumbnails) - minimal dark web interface -- self-hostable fastapi backend +- ultra-fast, low-memory Rust backend (Axum + Tokio) +- stream piping for flat memory footprint (~8MB idle RAM) +- automatic background cleanup of temporary downloads +- concurrency limiter to safeguard low-resource servers + +## backend (rust) + +```bash +cd backend + +# run in development +cargo run + +# or run optimized release +cargo run --release +``` + +Environment variables: +- `PORT` (default: `8000`) +- `HOST` (default: `0.0.0.0`) +- `MAX_CONCURRENT_DOWNLOADS` (default: `3`) +- `MAX_FILE_AGE_SECS` (default: `3600`) + +> Note: Legacy Python FastAPI backend is preserved in `backend-py/`. ## license diff --git a/backend/main.py b/backend-py/main.py similarity index 100% rename from backend/main.py rename to backend-py/main.py diff --git a/backend/requirements.txt b/backend-py/requirements.txt similarity index 100% rename from backend/requirements.txt rename to backend-py/requirements.txt diff --git a/backend/routers/info.py b/backend-py/routers/info.py similarity index 100% rename from backend/routers/info.py rename to backend-py/routers/info.py diff --git a/backend/routers/instagram.py b/backend-py/routers/instagram.py similarity index 100% rename from backend/routers/instagram.py rename to backend-py/routers/instagram.py diff --git a/backend/routers/youtube.py b/backend-py/routers/youtube.py similarity index 100% rename from backend/routers/youtube.py rename to backend-py/routers/youtube.py diff --git a/backend/services/instagram_service.py b/backend-py/services/instagram_service.py similarity index 100% rename from backend/services/instagram_service.py rename to backend-py/services/instagram_service.py diff --git a/backend/services/youtube_service.py b/backend-py/services/youtube_service.py similarity index 100% rename from backend/services/youtube_service.py rename to backend-py/services/youtube_service.py diff --git a/backend/utils.py b/backend-py/utils.py similarity index 100% rename from backend/utils.py rename to backend-py/utils.py diff --git a/backend/Cargo.lock b/backend/Cargo.lock new file mode 100644 index 0000000..24de702 --- /dev/null +++ b/backend/Cargo.lock @@ -0,0 +1,2000 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "alloc-no-stdlib" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e76a019e91224d279006ff972f1e984179a6e9feb050adba6ce8274aef23195" +dependencies = [ + "alloc-no-stdlib", +] + +[[package]] +name = "async-compression" +version = "0.4.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f10dafd0c8d2e51ae9a748805777613ed0bbe17bf586b76c8311f45c020a32f" +dependencies = [ + "compression-codecs", + "compression-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "axum" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "sync_wrapper", + "tokio", + "tower 0.5.3", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "brotli" +version = "8.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc91aac060a7a1e25823bdccbfb6af1875b88f17c6daac97894eed8207166b3" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "5.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a32acac15fe1967bc3986b2a6347dffc965602354ea6f450ad07e8bfd253583" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "005ec2760ca554fae18df7a11195552ec576cd665632a881bc011d5bb2fd4d80" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures", + "rand_core", +] + +[[package]] +name = "compression-codecs" +version = "0.4.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58a6d0db8759036a783bc7c3f7a07f8cef3bf9470eb1db3bc86e8bcd1c5d0fe8" +dependencies = [ + "brotli", + "compression-core", + "flate2", + "memchr", + "zstd", + "zstd-safe", +] + +[[package]] +name = "compression-core" +version = "0.4.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e8ccc4ea9f6acc32d102c0f6d471d11d913ad15f20c04de743374861fa1d414" + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "either" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" + +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "rand_core", + "wasm-bindgen", +] + +[[package]] +name = "h2" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8e5e5a340588f4452631496976cf8636d4a7ecf600239fdc27615d2530bc16" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "home" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "http-range-header" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9171a2ea8a68358193d15dd5d70c1c10a2afc3e7e4c5bc92bc9f025cebd7359c" + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b501faa50e7a26c3d3560ca625132f4078a17771f4810baf70475ae48cbe43" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0" + +[[package]] +name = "icu_properties" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa" + +[[package]] +name = "icu_provider" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce57d20d1ea864ce2ac172ab472d409214f4fd359f0b2a2775abdf522e2af99e" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "just-download-backend" +version = "2.0.0" +dependencies = [ + "axum", + "mime_guess", + "regex", + "reqwest", + "serde", + "serde_json", + "tokio", + "tokio-util", + "tower 0.4.13", + "tower-http 0.5.2", + "tracing", + "tracing-subscriber", + "uuid", + "which", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b18443e9c262bfe8fa82f51666e2642c53393f7e5c27b3e1aeab922cff5b9d8" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quinn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c1a41e437b6bbd489372cd4971de128e85c855f56c57f283d20ff016cf7c0a8" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04759210543be93709136e28212294a659ef5001836ff4eab4d663e4529bba83" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-util", + "tower 0.5.3", + "tower-http 0.6.11", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustls" +version = "0.23.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6725596c3f2c3a0aef021139e145d4eafe314a6623e4680ca83852b2c67ab2ba" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "serde_json" +version = "1.0.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba467056f1b547ed52077911161fc86985becbc60e8e1857c8a144dab0def891" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "thiserror" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tinystr" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cf0ded5c4e56918d8f8a339e1bb67d038d3bc6d144ac407904015ba2e4cde9b" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c85f2c3ef0b1cd58b36682f4b17aaa995f0e5db534d85692b4903abce21f67" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494815d09bf52b5548659851081238f0ca39ff638363907596da739561c62c52" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "libc", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" +dependencies = [ + "async-compression", + "bitflags", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower 0.5.3", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "unicase" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "uuid" +version = "1.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ef6dac1e96601b4fb3acccccff2139741fcb757cb9a36089bf5be91cfb285ce" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "rand", + "wasm-bindgen", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aecb87a33d3b0c5e3b7aa46336eaf486cffafbd281b195e4c8b80d50df2351bf" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef4c5d3d2cdf5c54f4231181768f5510842e350db025faf1f7163b1030ed928" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a690d511e3c1a8b3a55e33511e3c2c00c78415cd23650f32b808627f5696b9ed" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "411e4887f0071ef2d2164a9d5fdf2d20efbef78fccd3a78b0c10a1dc5295e48a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.5", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81941cd78d0c92026c33e5e01312845a4cb1e9af3407f9134b100dd03144103e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "web-sys" +version = "0.3.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbddc4a036f00ec4f18c83445bd3115cb306a91da554919a099d9222fe4a7f8" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "which" +version = "6.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f" +dependencies = [ + "either", + "home", + "rustix", + "winsafe", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "zlib-rs" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" + +[[package]] +name = "zstd" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf06bd8162af0734b344780deb55b42a2429ae430870d13fcc12f238e880fe6e" +dependencies = [ + "zstd-safe", +] + +[[package]] +name = "zstd-safe" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae42c0555055784c70058d19ba8e275528e8a99a706684868ace5da4e716a4ab" +dependencies = [ + "zstd-sys", +] + +[[package]] +name = "zstd-sys" +version = "2.1.0+zstd.1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef0a8027ec3ee71300ab3bcbcd0393f434aa72b91ca6d635a39941deae8eea0" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/backend/Cargo.toml b/backend/Cargo.toml new file mode 100644 index 0000000..38e739b --- /dev/null +++ b/backend/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "just-download-backend" +version = "2.0.0" +edition = "2021" +authors = ["oiupoyt"] +description = "Ultra-fast, memory-efficient Rust backend for just-download" + +[dependencies] +axum = { version = "0.7", default-features = false, features = ["tokio", "http1", "http2", "json"] } +tokio = { version = "1.38", features = ["full"] } +tokio-util = { version = "0.7", features = ["io"] } +tower = { version = "0.4", features = ["util"] } +tower-http = { version = "0.5", features = ["cors", "trace", "fs", "compression-full"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +regex = "1.10" +uuid = { version = "1.9", features = ["v4", "fast-rng"] } +reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json", "stream"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } +mime_guess = "2.0" +which = "6.0" + +[profile.release] +opt-level = 3 +lto = "fat" +codegen-units = 1 +panic = "abort" +strip = true diff --git a/backend/Makefile b/backend/Makefile new file mode 100644 index 0000000..61d8ada --- /dev/null +++ b/backend/Makefile @@ -0,0 +1,16 @@ +.PHONY: build release run clean check + +check: + cargo check + +build: + cargo build + +release: + cargo build --release + +run: + cargo run --release + +clean: + cargo clean diff --git a/backend/src/config.rs b/backend/src/config.rs new file mode 100644 index 0000000..9550467 --- /dev/null +++ b/backend/src/config.rs @@ -0,0 +1,62 @@ +use std::path::PathBuf; +use std::sync::Arc; +use tokio::sync::Semaphore; + +#[derive(Clone)] +pub struct AppConfig { + pub host: String, + pub port: u16, + pub download_dir: PathBuf, + pub max_file_age_secs: u64, + pub cleanup_interval_secs: u64, + pub semaphore: Arc, + pub ytdlp_bin: String, +} + +impl AppConfig { + pub fn from_env() -> Self { + let host = std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string()); + let port = std::env::var("PORT") + .ok() + .and_then(|p| p.parse().ok()) + .unwrap_or(8000); + let download_dir = PathBuf::from( + std::env::var("DOWNLOAD_DIR").unwrap_or_else(|_| "downloads".to_string()), + ); + let max_file_age_secs = std::env::var("MAX_FILE_AGE_SECS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(3600); // 1 hour + let cleanup_interval_secs = std::env::var("CLEANUP_INTERVAL_SECS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(600); // 10 minutes + + // Limit concurrent downloads on low-memory servers (default 2) + let max_concurrent = std::env::var("MAX_CONCURRENT_DOWNLOADS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(3); + + // Find yt-dlp binary + let ytdlp_bin = if let Ok(custom) = std::env::var("YTDLP_PATH") { + custom + } else if which::which("yt-dlp").is_ok() { + "yt-dlp".to_string() + } else if std::path::Path::new("./yt-dlp").exists() { + "./yt-dlp".to_string() + } else { + "yt-dlp".to_string() + }; + + Self { + host, + port, + download_dir, + max_file_age_secs, + cleanup_interval_secs, + semaphore: Arc::new(Semaphore::new(max_concurrent)), + ytdlp_bin, + } + } +} diff --git a/backend/src/error.rs b/backend/src/error.rs new file mode 100644 index 0000000..30a1532 --- /dev/null +++ b/backend/src/error.rs @@ -0,0 +1,60 @@ +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; +use axum::Json; +use serde_json::json; +use std::fmt; + +#[derive(Debug)] +pub enum AppError { + BadRequest(String), + NotFound(String), + Internal(String), + #[allow(dead_code)] + Timeout, +} + +impl fmt::Display for AppError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AppError::BadRequest(msg) => write!(f, "{}", msg), + AppError::NotFound(msg) => write!(f, "{}", msg), + AppError::Internal(msg) => write!(f, "{}", msg), + AppError::Timeout => write!(f, "Operation timed out"), + } + } +} + +impl std::error::Error for AppError {} + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + let (status, msg) = match self { + AppError::BadRequest(ref m) => (StatusCode::BAD_REQUEST, m.clone()), + AppError::NotFound(ref m) => (StatusCode::NOT_FOUND, m.clone()), + AppError::Internal(ref m) => (StatusCode::INTERNAL_SERVER_ERROR, m.clone()), + AppError::Timeout => (StatusCode::GATEWAY_TIMEOUT, "Operation timed out".to_string()), + }; + + // FastAPI standard format is {"detail": "..."} + let body = Json(json!({ "detail": msg })); + (status, body).into_response() + } +} + +impl From for AppError { + fn from(err: std::io::Error) -> Self { + AppError::Internal(err.to_string()) + } +} + +impl From for AppError { + fn from(err: serde_json::Error) -> Self { + AppError::Internal(format!("JSON error: {}", err)) + } +} + +impl From for AppError { + fn from(err: reqwest::Error) -> Self { + AppError::Internal(format!("Network error: {}", err)) + } +} diff --git a/backend/src/main.rs b/backend/src/main.rs new file mode 100644 index 0000000..7b3b4ee --- /dev/null +++ b/backend/src/main.rs @@ -0,0 +1,161 @@ +mod config; +mod error; +mod routes; +mod services; +mod utils; + +use axum::routing::{get, post}; +use axum::{Json, Router}; +use serde_json::json; +use std::net::SocketAddr; +use std::path::PathBuf; +use std::time::Duration; +use tower_http::compression::CompressionLayer; +use tower_http::cors::CorsLayer; +use tower_http::services::ServeDir; +use tower_http::trace::TraceLayer; +use tracing::{info, warn}; + +use crate::config::AppConfig; + +#[tokio::main] +async fn main() { + // Initialize logging + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "info,tower_http=info".into()), + ) + .init(); + + let config = AppConfig::from_env(); + + // Ensure downloads directory exists + if let Err(e) = tokio::fs::create_dir_all(&config.download_dir).await { + warn!("Failed to create download directory: {}", e); + } + + // Spawn periodic cleanup loop + let cleanup_dir = config.download_dir.clone(); + let max_age = config.max_file_age_secs; + let interval = config.cleanup_interval_secs; + tokio::spawn(async move { + run_cleanup_loop(cleanup_dir, max_age, interval).await; + }); + + // Build Axum routers + let info_routes = Router::new() + .route("/detect", post(routes::info::detect)) + .route("/preview", post(routes::info::preview)); + + let yt_routes = Router::new() + .route("/video", post(routes::youtube::video)) + .route("/audio", post(routes::youtube::audio)) + .route("/thumbnail", post(routes::youtube::thumbnail)) + .route("/subtitles", post(routes::youtube::subtitles)) + .route("/channel-art", post(routes::youtube::channel_art)); + + let ig_routes = Router::new() + .route("/post", post(routes::instagram::post)) + .route("/reel", post(routes::instagram::reel)) + .route("/profile", post(routes::instagram::profile)) + .route("/thumbnail", post(routes::instagram::thumbnail)); + + // Static file serving for /files/* + let serve_dir = ServeDir::new(&config.download_dir); + + let app = Router::new() + .route("/", get(root_handler)) + .route("/health", get(health_handler)) + .nest("/api/info", info_routes) + .nest("/api/youtube", yt_routes) + .nest("/api/instagram", ig_routes) + .nest_service("/files", serve_dir) + .layer(CorsLayer::permissive()) + .layer(CompressionLayer::new()) + .layer(TraceLayer::new_for_http()) + .with_state(config.clone()); + + let addr: SocketAddr = format!("{}:{}", config.host, config.port) + .parse() + .expect("Invalid host/port configuration"); + + info!("🚀 just-download Rust backend listening on http://{}", addr); + info!("⚡ yt-dlp binary resolved to: {}", config.ytdlp_bin); + + let listener = tokio::net::TcpListener::bind(addr) + .await + .unwrap_or_else(|e| panic!("Failed to bind to {}: {}", addr, e)); + + axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .await + .unwrap(); +} + +async fn root_handler() -> Json { + Json(json!({ + "status": "ok", + "message": "just download stuff lol (rust engine)", + "version": "2.0.0-rs" + })) +} + +async fn health_handler() -> Json { + Json(json!({ + "status": "healthy", + "engine": "rust", + "uptime": "ok" + })) +} + +/// Periodic background task that removes files older than max_age_secs +async fn run_cleanup_loop(dir: PathBuf, max_age_secs: u64, interval_secs: u64) { + let mut ticker = tokio::time::interval(Duration::from_secs(interval_secs)); + loop { + ticker.tick().await; + if let Ok(mut entries) = tokio::fs::read_dir(&dir).await { + let now = std::time::SystemTime::now(); + while let Ok(Some(entry)) = entries.next_entry().await { + if let Ok(meta) = entry.metadata().await { + if meta.is_file() { + if let Ok(modified) = meta.modified() { + if let Ok(elapsed) = now.duration_since(modified) { + if elapsed.as_secs() > max_age_secs { + let _ = tokio::fs::remove_file(entry.path()).await; + info!("Cleaned up expired file: {:?}", entry.path()); + } + } + } + } + } + } + } + } +} + +/// Graceful shutdown listener for SIGINT (Ctrl+C) and SIGTERM +async fn shutdown_signal() { + let ctrl_c = async { + tokio::signal::ctrl_c() + .await + .expect("Failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) + .expect("Failed to install SIGTERM handler") + .recv() + .await; + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } + info!("Shutdown signal received, draining active requests..."); +} diff --git a/backend/src/routes/info.rs b/backend/src/routes/info.rs new file mode 100644 index 0000000..ddfb238 --- /dev/null +++ b/backend/src/routes/info.rs @@ -0,0 +1,92 @@ +use axum::extract::State; +use axum::Json; +use regex::Regex; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use std::sync::LazyLock; + +use crate::config::AppConfig; +use crate::error::AppError; +use crate::services::instagram::InstagramService; +use crate::services::youtube::YouTubeService; +use crate::utils::{clean_ig_url, clean_yt_url, extract_ig_username, is_ig_profile_url}; + +static YT_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"(youtube\.com|youtu\.be)").expect("valid regex") +}); +static IG_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"(instagram\.com|instagr\.am)").expect("valid regex") +}); + +#[derive(Deserialize)] +pub struct InfoRequest { + pub url: String, +} + +#[derive(Serialize)] +pub struct DetectResponse { + pub platform: String, + pub url: String, +} + +pub fn detect_platform(url: &str) -> &'static str { + if YT_RE.is_match(url) { + "youtube" + } else if IG_RE.is_match(url) { + "instagram" + } else { + "unknown" + } +} + +pub async fn detect(Json(req): Json) -> Result, AppError> { + let platform = detect_platform(&req.url); + if platform == "unknown" { + return Err(AppError::BadRequest( + "could not detect platform from that url".to_string(), + )); + } + Ok(Json(DetectResponse { + platform: platform.to_string(), + url: req.url, + })) +} + +pub async fn preview( + State(config): State, + Json(req): Json, +) -> Result, AppError> { + let raw_url = req.url.trim(); + let platform = detect_platform(raw_url); + + let mut result_json = match platform { + "youtube" => { + let clean = clean_yt_url(raw_url); + let info = YouTubeService::fetch_info(&config.ytdlp_bin, &clean).await?; + serde_json::to_value(info)? + } + "instagram" => { + let clean = clean_ig_url(raw_url); + if is_ig_profile_url(&clean) { + let username = extract_ig_username(&clean) + .map_err(|e| AppError::BadRequest(e))?; + let info = InstagramService::fetch_profile_info(&config.ytdlp_bin, &username).await?; + serde_json::to_value(info)? + } else { + let info = InstagramService::fetch_post_info(&config.ytdlp_bin, &clean).await?; + serde_json::to_value(info)? + } + } + _ => { + return Err(AppError::BadRequest( + "unsupported platform — only YouTube and Instagram are supported".to_string(), + )); + } + }; + + if let Some(obj) = result_json.as_object_mut() { + obj.insert("platform".to_string(), json!(platform)); + } + + Ok(Json(result_json)) +} diff --git a/backend/src/routes/instagram.rs b/backend/src/routes/instagram.rs new file mode 100644 index 0000000..c64ced2 --- /dev/null +++ b/backend/src/routes/instagram.rs @@ -0,0 +1,123 @@ +use axum::extract::State; +use axum::response::Response; +use axum::Json; +use serde::Deserialize; + +use crate::config::AppConfig; +use crate::error::AppError; +use crate::services::instagram::InstagramService; +use crate::utils::{clean_ig_url, extract_ig_username}; + +#[derive(Deserialize)] +pub struct PostRequest { + pub url: String, + #[serde(default = "default_format")] + pub format: String, + pub start_time: Option, + pub end_time: Option, +} + +fn default_format() -> String { + "mp4".to_string() +} + +#[derive(Deserialize)] +pub struct ProfileRequest { + pub username: String, +} + +#[derive(Deserialize)] +pub struct SimpleRequest { + pub url: String, +} + +pub async fn post( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_ig_url(req.url.trim()); + let (path, filename) = InstagramService::download_post( + &config.ytdlp_bin, + &config.download_dir, + &url, + &req.format, + req.start_time.as_deref(), + req.end_time.as_deref(), + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn reel( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_ig_url(req.url.trim()); + let (path, filename) = InstagramService::download_reel( + &config.ytdlp_bin, + &config.download_dir, + &url, + req.start_time.as_deref(), + req.end_time.as_deref(), + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn profile( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let raw = req.username.trim(); + let username = if raw.contains("instagram.com") { + extract_ig_username(raw).map_err(|e| AppError::BadRequest(e))? + } else { + raw.trim_start_matches('@').to_string() + }; + + if username.is_empty() { + return Err(AppError::BadRequest("no username provided".to_string())); + } + + let (path, filename) = InstagramService::download_profile( + &config.ytdlp_bin, + &config.download_dir, + &username, + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn thumbnail( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_ig_url(req.url.trim()); + let (path, filename) = InstagramService::download_thumbnail( + &config.ytdlp_bin, + &config.download_dir, + &url, + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} diff --git a/backend/src/routes/mod.rs b/backend/src/routes/mod.rs new file mode 100644 index 0000000..382a5dc --- /dev/null +++ b/backend/src/routes/mod.rs @@ -0,0 +1,47 @@ +pub mod info; +pub mod instagram; +pub mod youtube; + +use axum::body::Body; +use axum::http::header::{CONTENT_DISPOSITION, CONTENT_LENGTH, CONTENT_TYPE}; +use axum::http::HeaderMap; +use axum::response::{IntoResponse, Response}; +use std::path::PathBuf; +use tokio_util::io::ReaderStream; + +use crate::error::AppError; + +/// Streams a file response with Content-Disposition and schedules background cleanup +pub async fn stream_file_response(path: PathBuf, filename: String) -> Result { + let file = tokio::fs::File::open(&path).await.map_err(|e| { + AppError::NotFound(format!("Failed to open generated file: {}", e)) + })?; + + let file_len = file.metadata().await.ok().map(|m| m.len()); + let stream = ReaderStream::new(file); + let body = Body::from_stream(stream); + + let mime = mime_guess::from_path(&filename) + .first_or_octet_stream() + .to_string(); + + let mut headers = HeaderMap::new(); + if let Ok(ct) = mime.parse() { + headers.insert(CONTENT_TYPE, ct); + } + if let Some(len) = file_len { + headers.insert(CONTENT_LENGTH, len.to_string().parse().unwrap()); + } + if let Ok(cd) = format!("attachment; filename=\"{}\"", filename).parse() { + headers.insert(CONTENT_DISPOSITION, cd); + } + + // Background deletion after 15 minutes (900 seconds) + let path_clone = path.clone(); + tokio::spawn(async move { + tokio::time::sleep(tokio::time::Duration::from_secs(900)).await; + let _ = tokio::fs::remove_file(&path_clone).await; + }); + + Ok((headers, body).into_response()) +} diff --git a/backend/src/routes/youtube.rs b/backend/src/routes/youtube.rs new file mode 100644 index 0000000..d2b0ce9 --- /dev/null +++ b/backend/src/routes/youtube.rs @@ -0,0 +1,166 @@ +use axum::extract::State; +use axum::response::Response; +use axum::Json; +use serde::Deserialize; +use serde_json::json; + +use crate::config::AppConfig; +use crate::error::AppError; +use crate::services::youtube::YouTubeService; +use crate::utils::clean_yt_url; + +#[derive(Deserialize)] +pub struct VideoRequest { + pub url: String, + #[serde(default = "default_resolution")] + pub resolution: String, + #[serde(default = "default_format")] + pub format: String, + pub start_time: Option, + pub end_time: Option, +} + +fn default_resolution() -> String { + "best".to_string() +} +fn default_format() -> String { + "mp4".to_string() +} + +#[derive(Deserialize)] +pub struct AudioRequest { + pub url: String, + #[serde(default = "default_quality")] + pub quality: String, + #[serde(default = "default_audio_format")] + pub format: String, +} + +fn default_quality() -> String { + "192".to_string() +} +fn default_audio_format() -> String { + "mp3".to_string() +} + +#[derive(Deserialize)] +pub struct SimpleRequest { + pub url: String, +} + +#[derive(Deserialize)] +pub struct SubtitleRequest { + pub url: String, + #[serde(default = "default_lang")] + pub lang: String, +} + +fn default_lang() -> String { + "en".to_string() +} + +#[derive(Deserialize)] +pub struct ChannelRequest { + pub channel_url: String, +} + +pub async fn video( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_yt_url(&req.url.trim()); + let (path, filename) = YouTubeService::download_video( + &config.ytdlp_bin, + &config.download_dir, + &url, + &req.resolution, + &req.format, + req.start_time.as_deref(), + req.end_time.as_deref(), + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn audio( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_yt_url(&req.url.trim()); + let (path, filename) = YouTubeService::download_audio( + &config.ytdlp_bin, + &config.download_dir, + &url, + &req.quality, + &req.format, + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn thumbnail( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_yt_url(&req.url.trim()); + let (path, filename) = YouTubeService::download_thumbnail( + &config.ytdlp_bin, + &config.download_dir, + &url, + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn subtitles( + State(config): State, + Json(req): Json, +) -> Result { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let url = clean_yt_url(&req.url.trim()); + let (path, filename) = YouTubeService::download_subtitles( + &config.ytdlp_bin, + &config.download_dir, + &url, + &req.lang, + ) + .await?; + + crate::routes::stream_file_response(path, filename).await +} + +pub async fn channel_art( + State(config): State, + Json(req): Json, +) -> Result, AppError> { + let _permit = config.semaphore.acquire().await.map_err(|_| { + AppError::Internal("Server busy, please retry shortly".to_string()) + })?; + + let assets = YouTubeService::download_channel_art( + &config.ytdlp_bin, + &config.download_dir, + req.channel_url.trim(), + ) + .await?; + + Ok(Json(json!({ "assets": assets }))) +} diff --git a/backend/src/services/instagram.rs b/backend/src/services/instagram.rs new file mode 100644 index 0000000..7961e74 --- /dev/null +++ b/backend/src/services/instagram.rs @@ -0,0 +1,210 @@ +use crate::error::AppError; +use crate::services::ytdlp::YtDlp; +use crate::utils::{format_duration, safe_filename}; +use serde::Serialize; +use std::path::{Path, PathBuf}; +use uuid::Uuid; + +#[derive(Serialize)] +pub struct IgPostInfoResponse { + #[serde(rename = "type")] + pub media_type: String, + pub title: String, + pub thumbnail: Option, + pub duration: Option, + pub duration_str: Option, + pub owner: Option, + pub likes: Option, +} + +#[derive(Serialize)] +pub struct IgProfileInfoResponse { + #[serde(rename = "type")] + pub media_type: String, + pub title: String, + pub thumbnail: Option, + pub owner: String, + pub likes: Option, +} + +pub struct InstagramService; + +impl InstagramService { + pub async fn fetch_post_info(bin: &str, url: &str) -> Result { + let json = YtDlp::dump_json(bin, url).await?; + + let title = json + .get("title") + .or_else(|| json.get("uploader")) + .and_then(|v| v.as_str()) + .unwrap_or("Instagram post") + .to_string(); + + let thumbnail = json.get("thumbnail").and_then(|v| v.as_str()).map(String::from); + let duration = json.get("duration").and_then(|v| v.as_f64()); + let duration_str = duration.map(|d| format_duration(Some(d))); + let owner = json.get("uploader").and_then(|v| v.as_str()).map(String::from); + let likes = json.get("like_count").and_then(|v| v.as_u64()); + + Ok(IgPostInfoResponse { + media_type: "video".to_string(), + title, + thumbnail, + duration, + duration_str, + owner, + likes, + }) + } + + pub async fn fetch_profile_info(bin: &str, username: &str) -> Result { + let profile_url = format!("https://www.instagram.com/{}/", username); + let json = YtDlp::dump_json(bin, &profile_url).await?; + + let thumbnail = json.get("thumbnail").and_then(|v| v.as_str()).map(String::from); + let followers = json.get("channel_follower_count").and_then(|v| v.as_u64()); + + Ok(IgProfileInfoResponse { + media_type: "profile".to_string(), + title: format!("@{}", username), + thumbnail, + owner: username.to_string(), + likes: followers, + }) + } + + pub async fn download_post( + bin: &str, + download_dir: &Path, + url: &str, + fmt: &str, + start_time: Option<&str>, + end_time: Option<&str>, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("ig_post_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let args = [ + "-f", "bestvideo+bestaudio/best", + "--merge-output-format", fmt, + "-o", &out_tmpl, + url, + ]; + + YtDlp::execute(bin, &args).await?; + + let mut final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound("Instagram post file not found".to_string()))?; + + if start_time.is_some() || end_time.is_some() { + let trimmed = download_dir.join(format!("ig_post_{}_trim.mp4", uid)); + if YtDlp::trim_media(&final_file, &trimmed, start_time, end_time).await.is_ok() { + let _ = tokio::fs::remove_file(&final_file).await; + final_file = trimmed; + } + } + + let download_name = format!("ig_post_{}.{}", uid, fmt); + Ok((final_file, download_name)) + } + + pub async fn download_reel( + bin: &str, + download_dir: &Path, + url: &str, + start_time: Option<&str>, + end_time: Option<&str>, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("ig_reel_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let args = [ + "-f", "bestvideo+bestaudio/best", + "--merge-output-format", "mp4", + "-o", &out_tmpl, + url, + ]; + + YtDlp::execute(bin, &args).await?; + + let mut final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound("Instagram reel file not found".to_string()))?; + + if start_time.is_some() || end_time.is_some() { + let trimmed = download_dir.join(format!("ig_reel_{}_trim.mp4", uid)); + if YtDlp::trim_media(&final_file, &trimmed, start_time, end_time).await.is_ok() { + let _ = tokio::fs::remove_file(&final_file).await; + final_file = trimmed; + } + } + + let download_name = format!("ig_reel_{}.mp4", uid); + Ok((final_file, download_name)) + } + + pub async fn download_thumbnail( + bin: &str, + download_dir: &Path, + url: &str, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let json = YtDlp::dump_json(bin, url).await?; + + let thumb_url = json + .get("thumbnail") + .and_then(|v| v.as_str()) + .ok_or_else(|| AppError::NotFound("No thumbnail found for Instagram post".to_string()))?; + + let filename = format!("ig_thumb_{}.jpg", uid); + let dest = download_dir.join(&filename); + + let client = reqwest::Client::new(); + let bytes = client + .get(thumb_url) + .send() + .await + .map_err(|e| AppError::Internal(format!("Failed to fetch thumbnail: {}", e)))? + .bytes() + .await + .map_err(|e| AppError::Internal(format!("Failed to read thumbnail bytes: {}", e)))?; + + tokio::fs::write(&dest, bytes).await?; + + Ok((dest, filename)) + } + + pub async fn download_profile( + bin: &str, + download_dir: &Path, + username: &str, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let profile_url = format!("https://www.instagram.com/{}/", username); + let json = YtDlp::dump_json(bin, &profile_url).await?; + + let thumb_url = json + .get("thumbnail") + .and_then(|v| v.as_str()) + .ok_or_else(|| AppError::NotFound(format!("Could not find profile picture for @{}", username)))?; + + let safe_user = safe_filename(username); + let filename = format!("{}_profile.jpg", safe_user); + let dest = download_dir.join(format!("ig_pfp_{}_{}.jpg", safe_user, uid)); + + let client = reqwest::Client::new(); + let bytes = client + .get(thumb_url) + .send() + .await + .map_err(|e| AppError::Internal(format!("Failed to fetch profile picture: {}", e)))? + .bytes() + .await + .map_err(|e| AppError::Internal(format!("Failed to read profile picture bytes: {}", e)))?; + + tokio::fs::write(&dest, bytes).await?; + + Ok((dest, filename)) + } +} diff --git a/backend/src/services/mod.rs b/backend/src/services/mod.rs new file mode 100644 index 0000000..b15945b --- /dev/null +++ b/backend/src/services/mod.rs @@ -0,0 +1,3 @@ +pub mod instagram; +pub mod youtube; +pub mod ytdlp; diff --git a/backend/src/services/youtube.rs b/backend/src/services/youtube.rs new file mode 100644 index 0000000..e6970fd --- /dev/null +++ b/backend/src/services/youtube.rs @@ -0,0 +1,336 @@ +use crate::error::AppError; +use crate::services::ytdlp::YtDlp; +use crate::utils::{format_duration, safe_filename}; +use serde::Serialize; +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; +use uuid::Uuid; + +#[derive(Serialize)] +pub struct YtInfoResponse { + pub title: Option, + pub duration: Option, + pub duration_str: String, + pub thumbnail: Option, + pub uploader: Option, + pub view_count: Option, + pub upload_date: Option, + pub available_resolutions: Vec, + pub fps_options: Vec, + pub has_subtitles: bool, + pub subtitle_langs: Vec, +} + +#[derive(Serialize)] +pub struct ChannelAsset { + #[serde(rename = "type")] + pub asset_type: String, + pub filename: String, + pub url: String, +} + +pub struct YouTubeService; + +impl YouTubeService { + pub async fn fetch_info(bin: &str, url: &str) -> Result { + let json = YtDlp::dump_json(bin, url).await?; + + let title = json.get("title").and_then(|v| v.as_str()).map(String::from); + let duration = json.get("duration").and_then(|v| v.as_f64()); + let duration_str = format_duration(duration); + let thumbnail = json.get("thumbnail").and_then(|v| v.as_str()).map(String::from); + let uploader = json.get("uploader").and_then(|v| v.as_str()).map(String::from); + let view_count = json.get("view_count").and_then(|v| v.as_u64()); + let upload_date = json.get("upload_date").and_then(|v| v.as_str()).map(String::from); + + let mut res_set = BTreeSet::new(); + let mut fps_set = BTreeSet::new(); + + if let Some(formats) = json.get("formats").and_then(|v| v.as_array()) { + for f in formats { + if let Some(h) = f.get("height").and_then(|v| v.as_u64()) { + res_set.insert(h); + } + if let Some(fps) = f.get("fps").and_then(|v| v.as_u64()) { + fps_set.insert(fps); + } + } + } + + let mut available_resolutions: Vec = res_set.into_iter().collect(); + available_resolutions.reverse(); + + let mut fps_options: Vec = fps_set.into_iter().collect(); + fps_options.reverse(); + + let mut subtitle_langs = Vec::new(); + let mut has_subtitles = false; + + if let Some(subs) = json.get("subtitles").and_then(|v| v.as_object()) { + has_subtitles = !subs.is_empty(); + for lang in subs.keys() { + subtitle_langs.push(lang.clone()); + } + } + + Ok(YtInfoResponse { + title, + duration, + duration_str, + thumbnail, + uploader, + view_count, + upload_date, + available_resolutions, + fps_options, + has_subtitles, + subtitle_langs, + }) + } + + pub async fn download_video( + bin: &str, + download_dir: &Path, + url: &str, + resolution: &str, + fmt: &str, + start_time: Option<&str>, + end_time: Option<&str>, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("yt_video_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let fmt_sel = if resolution == "best" { + format!("bestvideo[ext={}]+bestaudio/bestvideo+bestaudio/best", fmt) + } else { + format!( + "bestvideo[height<={}][ext={}]+bestaudio/bestvideo[height<={}]+bestaudio/best", + resolution, fmt, resolution + ) + }; + + let mut args: Vec<&str> = vec![ + "-f", &fmt_sel, + "--merge-output-format", fmt, + "-o", &out_tmpl, + ]; + + let start_owned; + let end_owned; + if let Some(st) = start_time { + start_owned = format!("*{}", st); + args.push("--download-sections"); + args.push(&start_owned); + } else if let Some(et) = end_time { + end_owned = format!("*-{}", et); + args.push("--download-sections"); + args.push(&end_owned); + } + + args.push(url); + + YtDlp::execute(bin, &args).await?; + + let final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound("Output video file not found".to_string()))?; + + // Extract video title + let title = match YtDlp::dump_json(bin, url).await { + Ok(json) => safe_filename(json.get("title").and_then(|v| v.as_str()).unwrap_or("video")), + Err(_) => "video".to_string(), + }; + + let ext = final_file.extension().and_then(|e| e.to_str()).unwrap_or(fmt); + let download_name = format!("{}.{}", title, ext); + + Ok((final_file, download_name)) + } + + pub async fn download_audio( + bin: &str, + download_dir: &Path, + url: &str, + quality: &str, + fmt: &str, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("yt_audio_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let codec = match fmt { + "opus" => "opus", + "flac" => "flac", + "m4a" => "m4a", + _ => "mp3", + }; + + let args = [ + "-x", + "--audio-format", codec, + "--audio-quality", quality, + "-o", &out_tmpl, + url, + ]; + + YtDlp::execute(bin, &args).await?; + + let final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound("Audio file not found".to_string()))?; + + let title = match YtDlp::dump_json(bin, url).await { + Ok(json) => safe_filename(json.get("title").and_then(|v| v.as_str()).unwrap_or("audio")), + Err(_) => "audio".to_string(), + }; + + let ext = final_file.extension().and_then(|e| e.to_str()).unwrap_or(codec); + let download_name = format!("{}.{}", title, ext); + + Ok((final_file, download_name)) + } + + pub async fn download_thumbnail( + bin: &str, + download_dir: &Path, + url: &str, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("yt_thumb_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let args = [ + "--skip-download", + "--write-thumbnail", + "-o", &out_tmpl, + url, + ]; + + YtDlp::execute(bin, &args).await?; + + let final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound("Thumbnail file not found".to_string()))?; + + let title = match YtDlp::dump_json(bin, url).await { + Ok(json) => safe_filename(json.get("title").and_then(|v| v.as_str()).unwrap_or("thumbnail")), + Err(_) => "thumbnail".to_string(), + }; + + let ext = final_file.extension().and_then(|e| e.to_str()).unwrap_or("jpg"); + let download_name = format!("{}_thumbnail.{}", title, ext); + + Ok((final_file, download_name)) + } + + pub async fn download_subtitles( + bin: &str, + download_dir: &Path, + url: &str, + lang: &str, + ) -> Result<(PathBuf, String), AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let out_prefix = format!("yt_subs_{}", uid); + let out_tmpl = format!("{}/{}.%(ext)s", download_dir.display(), out_prefix); + + let args = [ + "--skip-download", + "--write-subs", + "--write-auto-subs", + "--sub-lang", lang, + "--sub-format", "srt", + "-o", &out_tmpl, + url, + ]; + + YtDlp::execute(bin, &args).await?; + + let final_file = YtDlp::find_file_with_prefix(download_dir, &out_prefix) + .ok_or_else(|| AppError::NotFound(format!("No subtitles found for lang={}", lang)))?; + + let title = match YtDlp::dump_json(bin, url).await { + Ok(json) => safe_filename(json.get("title").and_then(|v| v.as_str()).unwrap_or("subtitles")), + Err(_) => "subtitles".to_string(), + }; + + let download_name = format!("{}.{}.srt", title, lang); + + Ok((final_file, download_name)) + } + + pub async fn download_channel_art( + bin: &str, + download_dir: &Path, + channel_url: &str, + ) -> Result, AppError> { + let uid = &Uuid::new_v4().to_string()[..10]; + let json = YtDlp::dump_json(bin, channel_url).await?; + + let channel_name = safe_filename( + json.get("channel") + .or_else(|| json.get("uploader")) + .or_else(|| json.get("title")) + .and_then(|v| v.as_str()) + .unwrap_or("channel"), + ); + + let mut assets = Vec::new(); + let client = reqwest::Client::new(); + + if let Some(thumbnails) = json.get("thumbnails").and_then(|v| v.as_array()) { + // Find avatar + let mut avatar_candidates = Vec::new(); + for t in thumbnails { + if let Some(u) = t.get("url").and_then(|v| v.as_str()) { + let w = t.get("width").and_then(|v| v.as_u64()).unwrap_or(0); + let h = t.get("height").and_then(|v| v.as_u64()).unwrap_or(0); + if w > 0 && w == h { + avatar_candidates.push((w, u)); + } + } + } + + if let Some((_, best_avatar_url)) = avatar_candidates.into_iter().max_by_key(|c| c.0) { + let filename = format!("{}_avatar_{}.jpg", channel_name, uid); + let dest = download_dir.join(&filename); + if let Ok(resp) = client.get(best_avatar_url).send().await { + if let Ok(bytes) = resp.bytes().await { + let _ = tokio::fs::write(&dest, bytes).await; + assets.push(ChannelAsset { + asset_type: "avatar".to_string(), + filename: filename.clone(), + url: format!("/files/{}", filename), + }); + } + } + } + + // Find banner + let mut banner_candidates = Vec::new(); + for t in thumbnails { + if let Some(u) = t.get("url").and_then(|v| v.as_str()) { + let w = t.get("width").and_then(|v| v.as_u64()).unwrap_or(0); + let h = t.get("height").and_then(|v| v.as_u64()).unwrap_or(0); + if w > 0 && h > 0 && w > h * 2 { + banner_candidates.push((w * h, u)); + } + } + } + + if let Some((_, best_banner_url)) = banner_candidates.into_iter().max_by_key(|c| c.0) { + let filename = format!("{}_banner_{}.jpg", channel_name, uid); + let dest = download_dir.join(&filename); + if let Ok(resp) = client.get(best_banner_url).send().await { + if let Ok(bytes) = resp.bytes().await { + let _ = tokio::fs::write(&dest, bytes).await; + assets.push(ChannelAsset { + asset_type: "banner".to_string(), + filename: filename.clone(), + url: format!("/files/{}", filename), + }); + } + } + } + } + + Ok(assets) + } +} diff --git a/backend/src/services/ytdlp.rs b/backend/src/services/ytdlp.rs new file mode 100644 index 0000000..92ff77a --- /dev/null +++ b/backend/src/services/ytdlp.rs @@ -0,0 +1,125 @@ +use crate::error::AppError; +use std::path::{Path, PathBuf}; +use std::process::Stdio; +use tokio::process::Command; +use tracing::{error, info}; + +pub struct YtDlp; + +impl YtDlp { + /// Detects if node runtime exists for yt-dlp javascript engine + fn has_node() -> bool { + which::which("node").is_ok() + } + + /// Spawns yt-dlp and extracts JSON metadata + pub async fn dump_json(bin: &str, url: &str) -> Result { + let mut cmd = Command::new(bin); + cmd.arg("--dump-json") + .arg("--skip-download") + .arg("--no-warnings") + .arg("--no-playlist"); + + if Self::has_node() { + cmd.arg("--js-runtimes").arg("node"); + } + + cmd.arg(url); + cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); + + let output = cmd.output().await.map_err(|e| { + AppError::Internal(format!("Failed to execute yt-dlp: {}", e)) + })?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + error!("yt-dlp error: {}", stderr); + return Err(AppError::Internal(format!( + "yt-dlp failed: {}", + stderr.lines().next().unwrap_or("Unknown error") + ))); + } + + let json_val: serde_json::Value = serde_json::from_slice(&output.stdout) + .map_err(|e| AppError::Internal(format!("Failed to parse yt-dlp metadata: {}", e)))?; + + Ok(json_val) + } + + /// Runs yt-dlp with custom arguments + pub async fn execute(bin: &str, args: &[&str]) -> Result<(), AppError> { + let mut cmd = Command::new(bin); + cmd.args(args); + cmd.arg("--no-warnings").arg("--no-playlist"); + + if Self::has_node() { + cmd.arg("--js-runtimes").arg("node"); + } + + cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); + + info!("Executing yt-dlp with {} args", args.len()); + let output = cmd.output().await.map_err(|e| { + AppError::Internal(format!("Failed to run yt-dlp: {}", e)) + })?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + error!("yt-dlp execution failed: {}", stderr); + return Err(AppError::Internal(format!( + "Download failed: {}", + stderr.lines().last().unwrap_or("yt-dlp error") + ))); + } + + Ok(()) + } + + /// Trims video/audio using ffmpeg stream copy + pub async fn trim_media( + src: &Path, + dest: &Path, + start_time: Option<&str>, + end_time: Option<&str>, + ) -> Result<(), AppError> { + let mut cmd = Command::new("ffmpeg"); + cmd.arg("-y"); + + if let Some(start) = start_time { + cmd.arg("-ss").arg(start); + } + cmd.arg("-i").arg(src); + + if let Some(end) = end_time { + cmd.arg("-to").arg(end); + } + cmd.arg("-c").arg("copy").arg(dest); + + cmd.stdout(Stdio::null()).stderr(Stdio::piped()); + + let output = cmd.output().await.map_err(|e| { + AppError::Internal(format!("Failed to execute ffmpeg: {}", e)) + })?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + return Err(AppError::Internal(format!("ffmpeg trim failed: {}", stderr))); + } + + Ok(()) + } + + /// Finds the first file in a directory that matches a prefix + pub fn find_file_with_prefix(dir: &Path, prefix: &str) -> Option { + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let name = entry.file_name(); + let name_str = name.to_string_lossy(); + if name_str.starts_with(prefix) && entry.path().is_file() { + return Some(entry.path()); + } + } + } + None + } +} diff --git a/backend/src/utils.rs b/backend/src/utils.rs new file mode 100644 index 0000000..69b7a51 --- /dev/null +++ b/backend/src/utils.rs @@ -0,0 +1,144 @@ +use regex::Regex; +use std::sync::LazyLock; + +static YT_V_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"[?&]v=([A-Za-z0-9_-]{11})").expect("valid regex") +}); +static YT_SHORT_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"https?://youtu\.be/([A-Za-z0-9_-]{11})").expect("valid regex") +}); +#[allow(dead_code)] +static IG_SHORTCODE_RE: LazyLock = LazyLock::new(|| { + Regex::new(r"/(?:p|reel|reels|tv)/([A-Za-z0-9_-]+)").expect("valid regex") +}); +static SAFE_RE: LazyLock = LazyLock::new(|| { + Regex::new(r#"[\\/*?:"<>|]"#).expect("valid regex") +}); + +pub fn clean_yt_url(url: &str) -> String { + if let Some(caps) = YT_V_RE.captures(url) { + if let Some(id) = caps.get(1) { + return format!("https://www.youtube.com/watch?v={}", id.as_str()); + } + } + if let Some(caps) = YT_SHORT_RE.captures(url) { + if let Some(id) = caps.get(1) { + return format!("https://youtu.be/{}", id.as_str()); + } + } + url.to_string() +} + +pub fn clean_ig_url(url: &str) -> String { + url.split('?').next().unwrap_or(url).trim_end_matches('/').to_string() +} + +pub fn is_ig_profile_url(url: &str) -> bool { + let stripped = url + .trim_start_matches("https://") + .trim_start_matches("http://") + .trim_start_matches("www.") + .trim_start_matches("instagram.com") + .trim_start_matches('/'); + + let first_segment = stripped + .split('/') + .next() + .unwrap_or("") + .split('?') + .next() + .unwrap_or("") + .trim_start_matches('@'); + + !matches!( + first_segment, + "p" | "reel" | "reels" | "tv" | "explore" | "stories" | "accounts" | "" + ) +} + +pub fn extract_ig_username(url: &str) -> Result { + let stripped = url + .trim_start_matches("https://") + .trim_start_matches("http://") + .trim_start_matches("www.") + .trim_start_matches("instagram.com") + .trim_start_matches('/'); + + let username = stripped + .split('/') + .next() + .unwrap_or("") + .split('?') + .next() + .unwrap_or("") + .trim_start_matches('@') + .trim(); + + if username.is_empty() { + Err("could not extract username from URL".to_string()) + } else { + Ok(username.to_string()) + } +} + +#[allow(dead_code)] +pub fn extract_ig_shortcode(url: &str) -> Result { + if let Some(caps) = IG_SHORTCODE_RE.captures(url) { + if let Some(sc) = caps.get(1) { + return Ok(sc.as_str().to_string()); + } + } + Err(format!( + "could not extract shortcode from: {} — expected a post, reel, or tv URL", + url + )) +} + +pub fn safe_filename(name: &str) -> String { + let sanitized = SAFE_RE.replace_all(name, "_"); + let trimmed = sanitized.trim(); + if trimmed.is_empty() { + "file".to_string() + } else if trimmed.len() > 80 { + trimmed[..80].to_string() + } else { + trimmed.to_string() + } +} + +pub fn format_duration(seconds: Option) -> String { + match seconds { + Some(s) if s > 0.0 => { + let total = s as u64; + let h = total / 3600; + let m = (total % 3600) / 60; + let sec = total % 60; + if h > 0 { + format!("{}:{:02}:{:02}", h, m, sec) + } else { + format!("{}:{:02}", m, sec) + } + } + _ => "0:00".to_string(), + } +} + +#[allow(dead_code)] +pub fn parse_time_to_seconds(t: &str) -> Option { + let parts: Vec<&str> = t.trim().split(':').collect(); + match parts.len() { + 3 => { + let h = parts[0].parse::().ok()?; + let m = parts[1].parse::().ok()?; + let s = parts[2].parse::().ok()?; + Some(h * 3600.0 + m * 60.0 + s) + } + 2 => { + let m = parts[0].parse::().ok()?; + let s = parts[1].parse::().ok()?; + Some(m * 60.0 + s) + } + 1 => parts[0].parse::().ok(), + _ => None, + } +}