commit 356c3273b0aca87303192ff98b5e90c8a19e2baf Author: OpenClaw Agent Date: Wed Sep 16 01:54:10 2026 +0800 feat: add standalone AM downloader with WebDAV sync diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0e57152 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.26-alpine AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY main.go index.html ./ +RUN CGO_ENABLED=0 go build -o /out/am-downloader ./main.go +FROM alpine:3.22 +WORKDIR /app +COPY --from=build /out/am-downloader /app/am-downloader +COPY index.html /app/index.html +RUN mkdir -p /data /downloads +ENV PORT=9540 DB_PATH=/data/downloader.db DOWNLOAD_DIR=/downloads AM_API_URL=http://127.0.0.1:9530/api WORKERS=2 +EXPOSE 9540 +CMD ["/app/am-downloader"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1e39121 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + am-downloader: + build: . + container_name: am-downloader + restart: unless-stopped + network_mode: host + environment: + PORT: "9540" + DB_PATH: /data/downloader.db + DOWNLOAD_DIR: /downloads + AM_API_URL: http://127.0.0.1:9530/api + WORKERS: "2" + volumes: + - ./data:/data + - ./downloads:/downloads \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5629685 --- /dev/null +++ b/go.mod @@ -0,0 +1,52 @@ +module am-downloader + +go 1.25.0 + +require ( + github.com/gin-gonic/gin v1.12.0 + github.com/glebarez/sqlite v1.11.0 + github.com/studio-b12/gowebdav v0.13.0 + gorm.io/gorm v1.31.2 +) + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/crypto v0.48.0 // indirect + golang.org/x/net v0.51.0 // indirect + golang.org/x/sys v0.41.0 // indirect + golang.org/x/text v0.34.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect + modernc.org/libc v1.22.5 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.5.0 // indirect + modernc.org/sqlite v1.23.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..37b09be --- /dev/null +++ b/go.sum @@ -0,0 +1,122 @@ +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= +github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/studio-b12/gowebdav v0.13.0 h1:OcwSg6IQHOFNdYHn3bPOHwSE8looG8N56Y5xTT1asqQ= +github.com/studio-b12/gowebdav v0.13.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= +golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= +golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= +gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo= +gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= +modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= +modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= +modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= +modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= diff --git a/index.html b/index.html new file mode 100644 index 0000000..2e2e715 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ +AM Downloader
AM Downloader
独立下载队列 · WebDAV NAS 同步
+
添加下载任务
后续 AM 的下载按钮将直接把歌曲加入此队列,浏览器不再承担大文件下载。
+
下载与 WebDAV
+
下载队列
加载中…
\ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..53eb589 --- /dev/null +++ b/main.go @@ -0,0 +1,364 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + "sync" + "time" + + "github.com/gin-gonic/gin" + "github.com/glebarez/sqlite" + "github.com/studio-b12/gowebdav" + "gorm.io/gorm" +) + +type Task struct { + ID uint `gorm:"primaryKey" json:"id"` + Source string `json:"source"` + RID string `json:"rid"` + Title string `json:"title"` + Artist string `json:"artist"` + Album string `json:"album"` + Pic string `json:"pic"` + Quality string `json:"quality"` + Format string `json:"format"` + Status string `json:"status"` + Error string `json:"error,omitempty"` + LocalPath string `json:"localPath,omitempty"` + RemotePath string `json:"remotePath,omitempty"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` +} + +type Setting struct { + ID uint `gorm:"primaryKey"` + Key string `gorm:"uniqueIndex"` + Value string +} +type SongRequest struct { + Source string `json:"source"` + RID string `json:"rid"` + Title string `json:"title"` + Artist string `json:"artist"` + Album string `json:"album"` + Pic string `json:"pic"` + Quality string `json:"quality"` +} +type Config struct { + AMAPI, DownloadDir, WebDAVURL, WebDAVUser, WebDAVPass, WebDAVPath, DefaultQuality string + WorkerCount int +} +type App struct { + db *gorm.DB + cfg Config + queue chan uint + wg sync.WaitGroup + httpClient *http.Client +} + +type playInfo struct { + URL string `json:"url"` + Format string `json:"format"` + Headers map[string]string `json:"headers"` +} + +func main() { + dbPath := getenv("DB_PATH", "/data/downloader.db") + _ = os.MkdirAll(filepath.Dir(dbPath), 0755) + db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) + must(err) + must(db.AutoMigrate(&Task{}, &Setting{})) + a := &App{db: db, cfg: Config{AMAPI: getenv("AM_API_URL", "http://127.0.0.1:9530/api"), DownloadDir: getenv("DOWNLOAD_DIR", "/downloads"), DefaultQuality: getenv("DEFAULT_QUALITY", "standard"), WorkerCount: atoi(getenv("WORKERS", "2"), 2)}, queue: make(chan uint, 100), httpClient: &http.Client{Timeout: 2 * time.Minute}} + _ = os.MkdirAll(a.cfg.DownloadDir, 0755) + a.loadSettings() + a.startWorkers() + r := gin.Default() + r.StaticFile("/", "/app/index.html") + api := r.Group("/api") + api.GET("/tasks", a.listTasks) + api.POST("/tasks", a.addTask) + api.POST("/tasks/:id/retry", a.retryTask) + api.DELETE("/tasks/:id", a.deleteTask) + api.GET("/settings", a.getSettings) + api.PUT("/settings", a.putSettings) + api.POST("/webdav/test", a.testWebDAV) + must(r.Run(":" + getenv("PORT", "9540"))) +} + +func (a *App) loadSettings() { + var ss []Setting + a.db.Find(&ss) + for _, s := range ss { + switch s.Key { + case "am_api": + a.cfg.AMAPI = s.Value + case "download_dir": + a.cfg.DownloadDir = s.Value + case "webdav_url": + a.cfg.WebDAVURL = s.Value + case "webdav_user": + a.cfg.WebDAVUser = s.Value + case "webdav_pass": + a.cfg.WebDAVPass = s.Value + case "webdav_path": + a.cfg.WebDAVPath = s.Value + case "quality": + a.cfg.DefaultQuality = s.Value + } + } +} +func (a *App) save(k, v string) { + var s Setting + if a.db.Where("key = ?", k).First(&s).Error != nil { + a.db.Create(&Setting{Key: k, Value: v}) + } else { + s.Value = v + a.db.Save(&s) + } +} +func (a *App) startWorkers() { + for i := 0; i < a.cfg.WorkerCount; i++ { + a.wg.Add(1) + go func() { + defer a.wg.Done() + for id := range a.queue { + a.download(id) + } + }() + } +} +func (a *App) addTask(c *gin.Context) { + var in SongRequest + if c.BindJSON(&in) != nil || in.RID == "" || in.Title == "" { + c.JSON(400, gin.H{"error": "source/rid/title required"}) + return + } + if in.Quality == "" { + in.Quality = a.cfg.DefaultQuality + } + t := Task{Source: in.Source, RID: in.RID, Title: in.Title, Artist: in.Artist, Album: in.Album, Pic: in.Pic, Quality: in.Quality, Status: "pending"} + if err := a.db.Create(&t).Error; err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + a.queue <- t.ID + c.JSON(202, t) +} +func (a *App) listTasks(c *gin.Context) { + var ts []Task + a.db.Order("id desc").Limit(100).Find(&ts) + c.JSON(200, gin.H{"data": ts}) +} +func (a *App) retryTask(c *gin.Context) { + id, _ := strconv.ParseUint(c.Param("id"), 10, 64) + var t Task + if a.db.First(&t, id).Error != nil { + c.Status(404) + return + } + a.db.Model(&t).Updates(map[string]any{"status": "pending", "error": ""}) + a.queue <- t.ID + c.JSON(202, t) +} +func (a *App) deleteTask(c *gin.Context) { + id, _ := strconv.ParseUint(c.Param("id"), 10, 64) + a.db.Delete(&Task{}, id) + c.Status(204) +} + +func (a *App) download(id uint) { + var t Task + if a.db.First(&t, id).Error != nil { + return + } + a.db.Model(&t).Updates(map[string]any{"status": "downloading", "error": ""}) + info, err := a.resolve(t) + if err != nil { + a.fail(&t, err) + return + } + ext := info.Format + if ext == "" { + ext = "mp3" + } + filename := safe(t.Artist) + " - " + safe(t.Title) + "." + safe(ext) + local := filepath.Join(a.cfg.DownloadDir, filename) + t.Format = ext + t.LocalPath = local + if _, err = os.Stat(local); err == nil { + a.upload(&t) + return + } + req, err := http.NewRequest("GET", info.URL, nil) + if err != nil { + a.fail(&t, err) + return + } + for k, v := range info.Headers { + req.Header.Set(k, v) + } + resp, err := a.httpClient.Do(req) + if err != nil { + a.fail(&t, err) + return + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + a.fail(&t, fmt.Errorf("media http %d", resp.StatusCode)) + return + } + f, err := os.Create(local) + if err != nil { + a.fail(&t, err) + return + } + _, err = io.Copy(f, resp.Body) + cerr := f.Close() + if err != nil { + a.fail(&t, err) + return + } + if cerr != nil { + a.fail(&t, cerr) + return + } + a.upload(&t) +} + +func (a *App) resolve(t Task) (playInfo, error) { + u, _ := url.Parse(strings.TrimRight(a.cfg.AMAPI, "/") + "/song/play-url") + q := u.Query() + q.Set("source", t.Source) + q.Set("rid", t.RID) + q.Set("level", t.Quality) + u.RawQuery = q.Encode() + resp, err := a.httpClient.Get(u.String()) + if err != nil { + return playInfo{}, err + } + defer resp.Body.Close() + var x struct { + Code int `json:"code"` + Data playInfo `json:"data"` + Message string `json:"message"` + } + if err = json.NewDecoder(resp.Body).Decode(&x); err != nil { + return playInfo{}, err + } + if x.Code != 0 || x.Data.URL == "" { + return playInfo{}, errors.New(x.Message) + } + return x.Data, nil +} + +func (a *App) upload(t *Task) { + if a.cfg.WebDAVURL == "" { + t.Status = "done" + a.db.Save(t) + return + } + a.db.Model(t).Updates(map[string]any{"status": "uploading"}) + remote := strings.TrimRight(a.cfg.WebDAVPath, "/") + "/" + safe(t.Artist) + " - " + safe(t.Title) + "." + safe(t.Format) + t.RemotePath = remote + client := gowebdav.NewClient(a.cfg.WebDAVURL, a.cfg.WebDAVUser, a.cfg.WebDAVPass) + client.SetTimeout(10 * time.Minute) + if err := client.MkdirAll(strings.TrimSuffix(remote, "/"), 0755); err != nil { + a.fail(t, err) + return + } + f, err := os.Open(t.LocalPath) + if err != nil { + a.fail(t, err) + return + } + defer f.Close() + if err = client.WriteStream(remote, f, 0644); err != nil { + a.fail(t, err) + return + } + t.Status = "done" + a.db.Save(t) +} +func (a *App) fail(t *Task, err error) { t.Status = "error"; t.Error = err.Error(); a.db.Save(t) } + +func (a *App) getSettings(c *gin.Context) { + c.JSON(200, gin.H{"amApi": a.cfg.AMAPI, "downloadDir": a.cfg.DownloadDir, "webdavUrl": a.cfg.WebDAVURL, "webdavUser": a.cfg.WebDAVUser, "webdavPass": a.cfg.WebDAVPass, "webdavPath": a.cfg.WebDAVPath, "quality": a.cfg.DefaultQuality}) +} +func (a *App) putSettings(c *gin.Context) { + var v map[string]string + if c.BindJSON(&v) != nil { + c.Status(400) + return + } + if x := v["amApi"]; x != "" { + a.cfg.AMAPI = x + a.save("am_api", x) + } + if x := v["downloadDir"]; x != "" { + a.cfg.DownloadDir = x + a.save("download_dir", x) + os.MkdirAll(x, 0755) + } + a.cfg.WebDAVURL = v["webdavUrl"] + a.cfg.WebDAVUser = v["webdavUser"] + a.cfg.WebDAVPass = v["webdavPass"] + a.cfg.WebDAVPath = strings.Trim(v["webdavPath"], "/") + a.cfg.DefaultQuality = v["quality"] + for k, x := range map[string]string{"webdav_url": a.cfg.WebDAVURL, "webdav_user": a.cfg.WebDAVUser, "webdav_pass": a.cfg.WebDAVPass, "webdav_path": a.cfg.WebDAVPath, "quality": a.cfg.DefaultQuality} { + a.save(k, x) + } + c.JSON(200, gin.H{"ok": true}) +} +func (a *App) testWebDAV(c *gin.Context) { + var v struct{ URL, User, Pass, Path string } + if c.BindJSON(&v) != nil { + c.Status(400) + return + } + client := gowebdav.NewClient(v.URL, v.User, v.Pass) + client.SetTimeout(20 * time.Second) + if err := client.Connect(); err != nil { + c.JSON(502, gin.H{"ok": false, "error": err.Error()}) + return + } + c.JSON(200, gin.H{"ok": true, "path": v.Path}) +} + +var bad = regexp.MustCompile(`[/\\:*?"<>|]+`) + +func safe(s string) string { + s = strings.TrimSpace(s) + s = bad.ReplaceAllString(s, "_") + s = strings.Trim(s, ". ") + if s == "" { + return "Unknown" + } + return s +} +func getenv(k, d string) string { + if v := os.Getenv(k); v != "" { + return v + } + return d +} +func atoi(s string, d int) int { + v, e := strconv.Atoi(s) + if e != nil || v < 1 { + return d + } + return v +} +func must(e error) { + if e != nil { + panic(e) + } +}