56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
###官方的文件分享池API
|
|
###By Liang-work(NRFF&nanci)
|
|
###Version 1.0
|
|
###License: BSD
|
|
|
|
DOMAIN = 'https://api.solian.app'
|
|
import base64
|
|
import os
|
|
from tusclient import client
|
|
import requests
|
|
import traceback
|
|
|
|
def DownloadFileFromServer(ID:str,SavePath:str='%temp%/') -> bytes:
|
|
"""
|
|
从服务器下载文件
|
|
:param ID: 文件ID
|
|
:param SavePath: 本地保存路径
|
|
:return:
|
|
"""
|
|
response = requests.get(f"{DOMAIN}/drive/files/{ID}")
|
|
if response.status_code == 200:
|
|
with open(SavePath, "wb") as f:
|
|
f.write(response.content)
|
|
return response.status_code
|
|
else:
|
|
return response.status_code
|
|
|
|
|
|
def UploadFileToServer(token:str,file_path,file_type,pool=1,chunk_size=1024*1024*10) ->str:
|
|
"""
|
|
使用tuspy上传文件
|
|
:param file_path: 文件路径
|
|
:param file_type: 文件类型 accept_types: ["image/*", "video/*", "audio/*"]
|
|
:param pool: 存储池(默认:Solar Network Shared)
|
|
:param chunk_size: 分块大小
|
|
:return: 文件id
|
|
"""
|
|
from tusclient import client
|
|
pools = [
|
|
"07bc8e33-f3aa-4e35-83c2-c63b2b67d8cd", # 雨云存储 中国
|
|
"c53136a6-9152-4ecb-9f88-43c41438c23e", # Solar Network Shared
|
|
"500e5ed8-bd44-4359-bc0a-ec85e2adf447", # Solar Network Driver
|
|
]
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"X-FilePool": pools[pool]
|
|
}
|
|
tus = client.TusClient('https://api.solian.app/drive/tus')
|
|
tus.set_headers(headers)
|
|
file = tus.uploader(file_path, chunk_size=chunk_size)
|
|
file.metadata = {
|
|
"content-type": file_type,
|
|
"filename": os.path.basename(file_path)
|
|
}
|
|
file.upload()
|
|
return file.url.split("/")[-1] |