FileCodeBox icon indicating copy to clipboard operation
FileCodeBox copied to clipboard

wget下载后不会保持源文件格式

Open Feng-Yong-Qi opened this issue 7 months ago • 4 comments

使用wget下载后的文件名称为:'select?code=95643',并不是文件原格式。我需要将:'select?code=95643' 修改为源文件格式才可以。比如源文件是个aaa.jpg,但是我wget下来的文件名是:'select?code=95643',我需要修改:mv 'select?code=95643' aaa.jpg 这样才可以

Feng-Yong-Qi avatar Apr 09 '25 08:04 Feng-Yong-Qi

感谢反馈

vastsa avatar Apr 09 '25 15:04 vastsa

代码中所有的file_storage.get_file_response已经正确添加了Content-Dispositionheader,所以正常来说不会有问题(wget 尊重这个 Header,会重新设置正确的文件名)

问题更加复杂

@share_api.get("/select/") 这里的定义中的路径定义是以斜杠结尾,fastapi 的默认行为是会发生一次重定向,即期待的行为是

curl https://share.lanol.cn/share/select?code=12345
# => 307 Temporary Redirect
curl https://share.lanol.cn/share/select/?code=12345

但是实际行为是

curl https://share.lanol.cn/share/select?code=12345
# => 307 Temporary Redirect
curl https://share.lanol.cn//share/select/?code=12345  # 出现了双斜杠,也就是说 wget 拿回来的是个 html 文件而不是附件,所以文件名会错误

这里重定向到错误 URL 的问题需要继续排查

curl 的结果如下

curl https://share.lanol.cn/share/select\?code\=xxxxx  -v
*   Trying ---
* Connected to share.lanol.cn (---) port 443
* ALPN: curl offers h2,http/1.1
* (304) (OUT), TLS handshake, Client hello (1):
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-AES256-GCM-SHA384
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=share.lanol.cn
*  start date: Apr  1 15:01:47 2025 GMT
*  expire date: Jun 30 15:01:46 2025 GMT
*  subjectAltName: host "share.lanol.cn" matched cert's "share.lanol.cn"
*  issuer: C=US; O=Let's Encrypt; CN=E5
*  SSL certificate verify ok.
* using HTTP/2
* [HTTP/2] [1] OPENED stream for https://share.lanol.cn/share/select?code=xxxxx
* [HTTP/2] [1] [:method: GET]
* [HTTP/2] [1] [:scheme: https]
* [HTTP/2] [1] [:authority: share.lanol.cn]
* [HTTP/2] [1] [:path: /share/select?code=xxxxx]
* [HTTP/2] [1] [user-agent: curl/8.4.0]
* [HTTP/2] [1] [accept: */*]
> GET /share/select?code=xxxxx HTTP/2
> Host: share.lanol.cn
> User-Agent: curl/8.4.0
> Accept: */*
> 
< HTTP/2 307 
< server: openresty
< date: Tue, 15 Apr 2025 06:33:41 GMT
< content-length: 0
< location: http://share.lanol.cn//share/select/?code=xxxxx
< x-cache: MISS
< cache-control: no-cache
< strict-transport-security: max-age=31536000
< 
* Connection #0 to host share.lanol.cn left intact

vccddd avatar Apr 15 '25 06:04 vccddd

可以让 <点击复制wget命令> 按钮生成的wget命令携带一个-O参数指定保存文件名

junchao98 avatar Apr 17 '25 08:04 junchao98

https://github.com/vastsa/FileCodeBox/blob/9a80d7a9e0bcdd9012527d543d8e1e0f7fdb0258/core/storage.py#L137

代码中使用的filename*而不是常用的filename,需要在wget命令加下述选项:

wget --content-disposition --trust-server-names "https://share.lanol.cn/share/select?code=12345"

Do1e avatar Jun 04 '25 03:06 Do1e