Compare commits
98 Commits
Author | SHA1 | Date | |
---|---|---|---|
38323fd24f | |||
366148a450 | |||
bc364cee0d | |||
f433277227 | |||
35fc1c87d2 | |||
9da2af8c49 | |||
103e049f22 | |||
cc217df924 | |||
939c9cd5ac | |||
6f0959a98e | |||
d71ed4d775 | |||
0af3e95f1f | |||
582f7bbfee | |||
cf2506901f | |||
db06b627cc | |||
5f2621eca9 | |||
3331462229 | |||
0f079827e5 | |||
ba66e33913 | |||
6a54ed87f3 | |||
88a9edb90a | |||
606134f39c | |||
3c03344ef1 | |||
6f5914ae6f | |||
4c00866249 | |||
04752f7473 | |||
26b4766da7 | |||
12af9cb89f | |||
958d793725 | |||
36f07ee194 | |||
91c2c21522 | |||
d6d2f52922 | |||
9162e782a0 | |||
dc41ceb99b | |||
c5e274f52a | |||
3781043c78 | |||
1485ab2677 | |||
337bf08cd3 | |||
22665aa19a | |||
1ab6b4e201 | |||
d97afb691b | |||
b63e65880f | |||
44cbe0522c | |||
fedab86c30 | |||
731dbf6c3a | |||
d00f75c814 | |||
f5b8815a84 | |||
99d06c7449 | |||
8e7b2c5837 | |||
3d3a97288a | |||
c2142cc03a | |||
3ce94de823 | |||
c64c003257 | |||
1c65588b4a | |||
d49f92b542 | |||
92a0453e00 | |||
bd7d27efc7 | |||
b2055777e0 | |||
fe79f9518b | |||
a7e9bb9e9a | |||
73d85d96f1 | |||
ff91d7a37d | |||
78f81ddc3b | |||
2f8258053f | |||
511efce624 | |||
14ff3450ab | |||
bbba161d55 | |||
6b61f8e9cc | |||
a295e7024a | |||
b36eaf08f0 | |||
bb6e520ab5 | |||
9b64e2e045 | |||
ee7c12c30f | |||
96d6d58910 | |||
2bf235a5ac | |||
236f9969c0 | |||
09e63027d9 | |||
a15dae291e | |||
efaaeedfb8 | |||
190c8001a5 | |||
b8698700ef | |||
985b81826f | |||
74d8fa3919 | |||
43e4928bb9 | |||
03580fd76c | |||
6e8d551420 | |||
28998d6f8c | |||
1779617cb9 | |||
7dfe48339c | |||
9c5627a382 | |||
809850321a | |||
bdc1f68746 | |||
9aaef6c3a3 | |||
bb50c52d0e | |||
6041e5a0fa | |||
308a86c36e | |||
ba7c4fc230 | |||
d81ec0637d |
39
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
39
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
name: "Bug report"
|
||||
description: Bug report
|
||||
labels: [pending triage]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thanks for taking the time to fill out this bug report!
|
||||
- type: input
|
||||
id: version
|
||||
attributes:
|
||||
label: Alist Version / Alist 版本
|
||||
description: What version of our software are you running?
|
||||
placeholder: v2.0.0
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: bug-description
|
||||
attributes:
|
||||
label: Describe the bug / 问题描述
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: reproduction
|
||||
attributes:
|
||||
label: Reproduction / 复现链接
|
||||
description: |
|
||||
Please provide a link to a repo that can reproduce the problem you ran into.
|
||||
请提供能复现此问题的链接
|
||||
validations:
|
||||
required: false
|
||||
- type: textarea
|
||||
id: logs
|
||||
attributes:
|
||||
label: 日志 / Logs
|
||||
description: |
|
||||
Please copy and paste any relevant log output.
|
||||
请复制粘贴错误日志,或者截图
|
||||
render: shell
|
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Questions & Discussions & Feature request
|
||||
url: https://github.com/Xhofe/alist/discussions
|
||||
about: Use GitHub discussions for message-board style questions and discussions or feature request.
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -21,8 +21,9 @@ dist/
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
bin/*
|
||||
alist
|
||||
/alist
|
||||
*.json
|
||||
public/index.html
|
||||
public/assets/
|
||||
public/public/
|
||||
data/
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Xhofe
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
78
README.md
Normal file → Executable file
78
README.md
Normal file → Executable file
@ -1,46 +1,72 @@
|
||||
<h2 align="center">Alist</h2>
|
||||
<p align="center">
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/release/Xhofe/alist?style=flat-square" alt="Release version"></a>
|
||||
<div align="center">
|
||||
<h3><a href="https://alist.nn.ci">Alist</a></h3>
|
||||
<p><em>🗂️Another file list program that supports multiple storage, powered by Gin and React.</em></p>
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/release/Xhofe/alist?style=flat-square" alt="latest version"></a>
|
||||
<a href="https://github.com/Xhofe/alist/discussions"><img src="https://img.shields.io/github/discussions/Xhofe/alist?color=%23ED8936&style=flat-square" alt="discussions"></a>
|
||||
<a href="https://github.com/Xhofe/alist/actions?query=workflow%3ABuild"><img src="https://img.shields.io/github/workflow/status/Xhofe/alist/build?style=flat-square" alt="Build status"></a>
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/downloads/Xhofe/alist/total?style=flat-square" alt="Downloads"></a>
|
||||
<a href="https://github.com/Xhofe/alist/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Xhofe/alist?style=flat-square" alt="License"></a>
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/downloads/Xhofe/alist/total?style=flat-square&color=%239F7AEA" alt="Downloads"></a>
|
||||
<a href="https://github.com/Xhofe/alist/blob/v2/LICENSE"><img src="https://img.shields.io/github/license/Xhofe/alist?style=flat-square" alt="License"></a>
|
||||
<a href="https://pay.xhofe.top">
|
||||
<img src="https://img.shields.io/badge/%24-donate-ff69b4.svg?style=flat-square" alt="donate">
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
---
|
||||
|
||||
### 这是什么?
|
||||
English | [中文](./README_cn.md)
|
||||
|
||||
一款支持多种存储的目录文件列表程序,后端基于`gin`,前端使用`react`。
|
||||
## Features
|
||||
|
||||
### 前端项目地址
|
||||
- [x] multiple storage
|
||||
- [x] Local storage
|
||||
- [x] [aliyundrive](https://www.aliyundrive.com/)
|
||||
- [x] OneDrive / Sharepoint ([global](https://www.office.com/), [cn](https://portal.partner.microsoftonline.cn),de,us)
|
||||
- [x] [189cloud](https://cloud.189.cn)
|
||||
- [x] [GoogleDrive](https://drive.google.com/)
|
||||
- [x] [123pan](https://www.123pan.com/)
|
||||
- [x] [lanzou](https://pc.woozooo.com/)
|
||||
- [x] [Alist](https://github.com/Xhofe/alist)
|
||||
- [x] FTP
|
||||
- [x] [PikPak](https://www.mypikpak.com/)
|
||||
- [x] [ShandianPan](https://shandianpan.com/)
|
||||
- [x] [S3](https://aws.amazon.com/s3/)
|
||||
- [x] WebDav
|
||||
- [x] Easy to deploy and out-of-the-box
|
||||
- [x] File preview (PDF, markdown, code, plain text, ...)
|
||||
- [x] Image preview in gallery mode
|
||||
- [x] Video and audio preview (mp4, mp3, ...)
|
||||
- [x] Office documents preview (docx, pptx, xlsx, ...)
|
||||
- [x] `README.md` preview rendering
|
||||
- [x] File permalink copy and direct file download
|
||||
- [x] Dark mode
|
||||
- [x] I18n
|
||||
- [x] Protected routes (password protection and authentication)
|
||||
- [x] WebDav (A small part readonly, see https://alist-doc.nn.ci/en/docs/intro for details)
|
||||
- [x] [Docker Deploy](https://hub.docker.com/r/xhofe/alist)
|
||||
- [x] Cloudflare workers proxy
|
||||
- [x] File/Folder package download
|
||||
- [x] Support video list playback and subtitles(ass,srt,vtt)
|
||||
- [x] Web upload(Can allow visitors to upload)
|
||||
|
||||
- https://github.com/Xhofe/alist-web
|
||||
## Discussion
|
||||
|
||||
### 演示地址
|
||||
Please go to our [discussion forum](https://github.com/Xhofe/alist/discussions) for general questions, **issues are for bug reports only.**
|
||||
|
||||
- https://alist.nn.ci
|
||||
## Demo
|
||||
|
||||
### 预览
|
||||
Available at: <https://alist.nn.ci>.
|
||||
|
||||
<a href="https://alist.nn.ci/"><img src="https://store.heytapimage.com/cdo-portal/feedback/202111/03/695ef77854a144e928518efde38db97a.png"></a>
|
||||

|
||||
|
||||
### 支持的存储
|
||||
## Document
|
||||
|
||||
- 本地存储
|
||||
- 阿里云盘
|
||||
- Onedrive/世纪互联
|
||||
- 天翼云盘
|
||||
- GoogleDrive
|
||||
- 123pan
|
||||
- ...
|
||||
<https://alist-doc.nn.ci/en/>
|
||||
|
||||
### 如何使用
|
||||
## License
|
||||
|
||||
- https://alist-doc.nn.ci/
|
||||
The `AList` is open-source software licensed under the MIT license.
|
||||
|
||||
### License
|
||||
---
|
||||
|
||||
The `AList` is open-source software licensed under the MIT license.
|
||||
> [@Blog](https://www.nn.ci/) · [@GitHub](https://github.com/Xhofe)
|
71
README_cn.md
Normal file
71
README_cn.md
Normal file
@ -0,0 +1,71 @@
|
||||
<div align="center">
|
||||
<h3><a href="https://alist.nn.ci">Alist</a></h3>
|
||||
<p><em>🗂️一个支持多存储的文件列表程序,使用 Gin 和 React 。</em></p>
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/release/Xhofe/alist?style=flat-square" alt="latest version"></a>
|
||||
<a href="https://github.com/Xhofe/alist/discussions"><img src="https://img.shields.io/github/discussions/Xhofe/alist?color=%23ED8936&style=flat-square" alt="discussions"></a>
|
||||
<a href="https://github.com/Xhofe/alist/actions?query=workflow%3ABuild"><img src="https://img.shields.io/github/workflow/status/Xhofe/alist/build?style=flat-square" alt="Build status"></a>
|
||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/downloads/Xhofe/alist/total?style=flat-square&color=%239F7AEA" alt="Downloads"></a>
|
||||
<a href="https://github.com/Xhofe/alist/blob/v2/LICENSE"><img src="https://img.shields.io/github/license/Xhofe/alist?style=flat-square" alt="License"></a>
|
||||
<a href="https://pay.xhofe.top">
|
||||
<img src="https://img.shields.io/badge/%24-donate-ff69b4.svg?style=flat-square" alt="donate">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
---
|
||||
|
||||
[English](./README.md) | 中文
|
||||
|
||||
## 支持
|
||||
|
||||
- [x] 多种存储
|
||||
- [x] 本地存储
|
||||
- [x] [阿里云盘](https://www.aliyundrive.com/)
|
||||
- [x] OneDrive / Sharepoint([国际版](https://www.office.com/), [世纪互联](https://portal.partner.microsoftonline.cn),de,us)
|
||||
- [x] [天翼云盘](https://cloud.189.cn)
|
||||
- [x] [GoogleDrive](https://drive.google.com/)
|
||||
- [x] [123云盘](https://www.123pan.com/)
|
||||
- [x] [蓝奏云](https://pc.woozooo.com/)
|
||||
- [x] [Alist](https://github.com/Xhofe/alist)
|
||||
- [x] FTP
|
||||
- [x] [PikPak](https://www.mypikpak.com/)
|
||||
- [x] [闪电盘](https://shandianpan.com/)
|
||||
- [x] [S3](https://aws.amazon.com/cn/s3/)
|
||||
- [x] WebDav
|
||||
- [x] 部署方便,开箱即用
|
||||
- [x] 文件预览(PDF、markdown、代码、纯文本……)
|
||||
- [x] 画廊模式下的图像预览
|
||||
- [x] 视频和音频预览(mp4、mp3 等)
|
||||
- [x] Office 文档预览(docx、pptx、xlsx、...)
|
||||
- [x] `README.md` 预览渲染
|
||||
- [x] 文件永久链接复制和直接文件下载
|
||||
- [x] 黑暗模式
|
||||
- [x] 国际化
|
||||
- [x] 受保护的路由(密码保护和身份验证)
|
||||
- [x] WebDav(少部分只读,具体见https://alist-doc.nn.ci/docs/intro )
|
||||
- [x] [Docker 部署](https://hub.docker.com/r/xhofe/alist)
|
||||
- [x] Cloudflare workers 中转
|
||||
- [x] 文件/文件夹打包下载
|
||||
- [x] 支持视频列表播放和字幕(ass,srt,vtt)
|
||||
- [x] 网页上传(可以允许访客上传)
|
||||
|
||||
## 讨论
|
||||
|
||||
一般问题请到[讨论论坛](https://github.com/Xhofe/alist/discussions) ,**issue仅针对错误报告。**
|
||||
|
||||
## 演示
|
||||
|
||||
<https://alist.nn.ci>。
|
||||
|
||||

|
||||
|
||||
## 文档
|
||||
|
||||
<https://alist-doc.nn.ci/>
|
||||
|
||||
## 许可
|
||||
|
||||
`AList` 是在 MIT 许可下许可的开源软件。
|
||||
|
||||
---
|
||||
|
||||
> [@Blog](https://www.nn.ci/) · [@GitHub](https://github.com/Xhofe)
|
430
alist-proxy.js
Normal file
430
alist-proxy.js
Normal file
@ -0,0 +1,430 @@
|
||||
const HOST = "YOUR_HOST";
|
||||
const TOKEN = "YOUR_TOKEN";
|
||||
|
||||
addEventListener("fetch", (event) => {
|
||||
const request = event.request;
|
||||
const url = new URL(request.url);
|
||||
const sign = url.searchParams.get("sign");
|
||||
if (request.method === "OPTIONS") {
|
||||
// Handle CORS preflight requests
|
||||
event.respondWith(handleOptions(request));
|
||||
} else if (sign && sign.length === 16) {
|
||||
// Handle requests to the Down server
|
||||
event.respondWith(handleDownload(request));
|
||||
} else {
|
||||
// Handle requests to the API server
|
||||
event.respondWith(handleRequest(event));
|
||||
}
|
||||
});
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
|
||||
"Access-Control-Max-Age": "86400",
|
||||
};
|
||||
|
||||
async function handleDownload(request) {
|
||||
const origin = request.headers.get("origin");
|
||||
const url = new URL(request.url);
|
||||
const path = decodeURI(url.pathname);
|
||||
const sign = url.searchParams.get("sign");
|
||||
const name = path.split("/").pop();
|
||||
const right = md5(`alist-${TOKEN}-${name}`).slice(8, 24);
|
||||
if (sign !== right) {
|
||||
const resp = new Response(
|
||||
JSON.stringify({
|
||||
code: 401,
|
||||
message: `sign mismatch`,
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json;charset=UTF-8",
|
||||
},
|
||||
}
|
||||
);
|
||||
resp.headers.set("Access-Control-Allow-Origin", origin);
|
||||
return resp;
|
||||
}
|
||||
|
||||
let resp = await fetch(`${HOST}/api/admin/link`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json;charset=UTF-8",
|
||||
Authorization: TOKEN,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
path: path,
|
||||
}),
|
||||
});
|
||||
let res = await resp.json();
|
||||
if (res.code !== 200) {
|
||||
return new Response(JSON.stringify(res));
|
||||
}
|
||||
request = new Request(res.data.url, request);
|
||||
if (res.data.headers) {
|
||||
for (const header of res.data.headers) {
|
||||
request.headers.set(header.name, header.value);
|
||||
}
|
||||
}
|
||||
let response = await fetch(request);
|
||||
|
||||
// Recreate the response so we can modify the headers
|
||||
response = new Response(response.body, response);
|
||||
|
||||
// Set CORS headers
|
||||
response.headers.set("Access-Control-Allow-Origin", origin);
|
||||
|
||||
// Append to/Add Vary header so browser will cache response correctly
|
||||
response.headers.append("Vary", "Origin");
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond to the request
|
||||
* @param {Request} request
|
||||
*/
|
||||
async function handleRequest(event) {
|
||||
const { request } = event;
|
||||
|
||||
//请求头部、返回对象
|
||||
let reqHeaders = new Headers(request.headers),
|
||||
outBody,
|
||||
outStatus = 200,
|
||||
outStatusText = "OK",
|
||||
outCt = null,
|
||||
outHeaders = new Headers({
|
||||
"Access-Control-Allow-Origin": reqHeaders.get("Origin"),
|
||||
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
||||
"Access-Control-Allow-Headers":
|
||||
reqHeaders.get("Access-Control-Allow-Headers") ||
|
||||
"Accept, Authorization, Cache-Control, Content-Type, DNT, If-Modified-Since, Keep-Alive, Origin, User-Agent, X-Requested-With, Token, x-access-token, Notion-Version",
|
||||
});
|
||||
|
||||
try {
|
||||
//取域名第一个斜杠后的所有信息为代理链接
|
||||
let url = request.url.substr(8);
|
||||
url = decodeURIComponent(url.substr(url.indexOf("/") + 1));
|
||||
|
||||
//需要忽略的代理
|
||||
if (
|
||||
request.method == "OPTIONS" &&
|
||||
reqHeaders.has("access-control-request-headers")
|
||||
) {
|
||||
//输出提示
|
||||
return new Response(null, PREFLIGHT_INIT);
|
||||
} else if (
|
||||
url.length < 3 ||
|
||||
url.indexOf(".") == -1 ||
|
||||
url == "favicon.ico" ||
|
||||
url == "robots.txt"
|
||||
) {
|
||||
return Response.redirect("https://baidu.com", 301);
|
||||
}
|
||||
//阻断
|
||||
else if (blocker.check(url)) {
|
||||
return Response.redirect("https://baidu.com", 301);
|
||||
} else {
|
||||
//补上前缀 http://
|
||||
url = url
|
||||
.replace(/https:(\/)*/, "https://")
|
||||
.replace(/http:(\/)*/, "http://");
|
||||
if (url.indexOf("://") == -1) {
|
||||
url = "http://" + url;
|
||||
}
|
||||
//构建 fetch 参数
|
||||
let fp = {
|
||||
method: request.method,
|
||||
headers: {},
|
||||
};
|
||||
|
||||
//保留头部其它信息
|
||||
let he = reqHeaders.entries();
|
||||
for (let h of he) {
|
||||
if (!["content-length"].includes(h[0])) {
|
||||
fp.headers[h[0]] = h[1];
|
||||
}
|
||||
}
|
||||
// 是否带 body
|
||||
if (["POST", "PUT", "PATCH", "DELETE"].indexOf(request.method) >= 0) {
|
||||
const ct = (reqHeaders.get("content-type") || "").toLowerCase();
|
||||
if (ct.includes("application/json")) {
|
||||
let requestJSON = await request.json();
|
||||
console.log(typeof requestJSON);
|
||||
fp.body = JSON.stringify(requestJSON);
|
||||
} else if (
|
||||
ct.includes("application/text") ||
|
||||
ct.includes("text/html")
|
||||
) {
|
||||
fp.body = await request.text();
|
||||
} else if (ct.includes("form")) {
|
||||
// fp.body = await request.formData();
|
||||
fp.body = await request.text();
|
||||
} else {
|
||||
fp.body = await request.blob();
|
||||
}
|
||||
}
|
||||
// 发起 fetch
|
||||
let fr = await fetch(url, fp);
|
||||
outCt = fr.headers.get("content-type");
|
||||
if (outCt.includes("application/text") || outCt.includes("text/html")) {
|
||||
try {
|
||||
// 添加base
|
||||
let newFr = new HTMLRewriter()
|
||||
.on("head", {
|
||||
element(element) {
|
||||
element.prepend(`<base href="${url}" />`, {
|
||||
html: true,
|
||||
});
|
||||
},
|
||||
})
|
||||
.transform(fr);
|
||||
fr = newFr;
|
||||
} catch (e) {}
|
||||
}
|
||||
outStatus = fr.status;
|
||||
outStatusText = fr.statusText;
|
||||
outBody = fr.body;
|
||||
}
|
||||
} catch (err) {
|
||||
outCt = "application/json";
|
||||
outBody = JSON.stringify({
|
||||
code: -1,
|
||||
msg: JSON.stringify(err.stack) || err,
|
||||
});
|
||||
}
|
||||
|
||||
//设置类型
|
||||
if (outCt && outCt != "") {
|
||||
outHeaders.set("content-type", outCt);
|
||||
}
|
||||
|
||||
let response = new Response(outBody, {
|
||||
status: outStatus,
|
||||
statusText: outStatusText,
|
||||
headers: outHeaders,
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
const blocker = {
|
||||
keys: [],
|
||||
check: function (url) {
|
||||
url = url.toLowerCase();
|
||||
let len = blocker.keys.filter((x) => url.includes(x)).length;
|
||||
return len != 0;
|
||||
},
|
||||
};
|
||||
|
||||
function handleOptions(request) {
|
||||
// Make sure the necessary headers are present
|
||||
// for this to be a valid pre-flight request
|
||||
let headers = request.headers;
|
||||
if (
|
||||
headers.get("Origin") !== null &&
|
||||
headers.get("Access-Control-Request-Method") !== null
|
||||
// && headers.get("Access-Control-Request-Headers") !== null
|
||||
) {
|
||||
// Handle CORS pre-flight request.
|
||||
// If you want to check or reject the requested method + headers
|
||||
// you can do that here.
|
||||
let respHeaders = {
|
||||
...corsHeaders,
|
||||
// Allow all future content Request headers to go back to browser
|
||||
// such as Authorization (Bearer) or X-Client-Name-Version
|
||||
"Access-Control-Allow-Headers": request.headers.get(
|
||||
"Access-Control-Request-Headers"
|
||||
),
|
||||
};
|
||||
|
||||
return new Response(null, {
|
||||
headers: respHeaders,
|
||||
});
|
||||
} else {
|
||||
// Handle standard OPTIONS request.
|
||||
// If you want to allow other HTTP Methods, you can do that here.
|
||||
return new Response(null, {
|
||||
headers: {
|
||||
Allow: "GET, HEAD, POST, OPTIONS",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
!(function (a) {
|
||||
"use strict";
|
||||
function b(a, b) {
|
||||
var c = (65535 & a) + (65535 & b),
|
||||
d = (a >> 16) + (b >> 16) + (c >> 16);
|
||||
return (d << 16) | (65535 & c);
|
||||
}
|
||||
function c(a, b) {
|
||||
return (a << b) | (a >>> (32 - b));
|
||||
}
|
||||
function d(a, d, e, f, g, h) {
|
||||
return b(c(b(b(d, a), b(f, h)), g), e);
|
||||
}
|
||||
function e(a, b, c, e, f, g, h) {
|
||||
return d((b & c) | (~b & e), a, b, f, g, h);
|
||||
}
|
||||
function f(a, b, c, e, f, g, h) {
|
||||
return d((b & e) | (c & ~e), a, b, f, g, h);
|
||||
}
|
||||
function g(a, b, c, e, f, g, h) {
|
||||
return d(b ^ c ^ e, a, b, f, g, h);
|
||||
}
|
||||
function h(a, b, c, e, f, g, h) {
|
||||
return d(c ^ (b | ~e), a, b, f, g, h);
|
||||
}
|
||||
function i(a, c) {
|
||||
(a[c >> 5] |= 128 << c % 32), (a[(((c + 64) >>> 9) << 4) + 14] = c);
|
||||
var d,
|
||||
i,
|
||||
j,
|
||||
k,
|
||||
l,
|
||||
m = 1732584193,
|
||||
n = -271733879,
|
||||
o = -1732584194,
|
||||
p = 271733878;
|
||||
for (d = 0; d < a.length; d += 16)
|
||||
(i = m),
|
||||
(j = n),
|
||||
(k = o),
|
||||
(l = p),
|
||||
(m = e(m, n, o, p, a[d], 7, -680876936)),
|
||||
(p = e(p, m, n, o, a[d + 1], 12, -389564586)),
|
||||
(o = e(o, p, m, n, a[d + 2], 17, 606105819)),
|
||||
(n = e(n, o, p, m, a[d + 3], 22, -1044525330)),
|
||||
(m = e(m, n, o, p, a[d + 4], 7, -176418897)),
|
||||
(p = e(p, m, n, o, a[d + 5], 12, 1200080426)),
|
||||
(o = e(o, p, m, n, a[d + 6], 17, -1473231341)),
|
||||
(n = e(n, o, p, m, a[d + 7], 22, -45705983)),
|
||||
(m = e(m, n, o, p, a[d + 8], 7, 1770035416)),
|
||||
(p = e(p, m, n, o, a[d + 9], 12, -1958414417)),
|
||||
(o = e(o, p, m, n, a[d + 10], 17, -42063)),
|
||||
(n = e(n, o, p, m, a[d + 11], 22, -1990404162)),
|
||||
(m = e(m, n, o, p, a[d + 12], 7, 1804603682)),
|
||||
(p = e(p, m, n, o, a[d + 13], 12, -40341101)),
|
||||
(o = e(o, p, m, n, a[d + 14], 17, -1502002290)),
|
||||
(n = e(n, o, p, m, a[d + 15], 22, 1236535329)),
|
||||
(m = f(m, n, o, p, a[d + 1], 5, -165796510)),
|
||||
(p = f(p, m, n, o, a[d + 6], 9, -1069501632)),
|
||||
(o = f(o, p, m, n, a[d + 11], 14, 643717713)),
|
||||
(n = f(n, o, p, m, a[d], 20, -373897302)),
|
||||
(m = f(m, n, o, p, a[d + 5], 5, -701558691)),
|
||||
(p = f(p, m, n, o, a[d + 10], 9, 38016083)),
|
||||
(o = f(o, p, m, n, a[d + 15], 14, -660478335)),
|
||||
(n = f(n, o, p, m, a[d + 4], 20, -405537848)),
|
||||
(m = f(m, n, o, p, a[d + 9], 5, 568446438)),
|
||||
(p = f(p, m, n, o, a[d + 14], 9, -1019803690)),
|
||||
(o = f(o, p, m, n, a[d + 3], 14, -187363961)),
|
||||
(n = f(n, o, p, m, a[d + 8], 20, 1163531501)),
|
||||
(m = f(m, n, o, p, a[d + 13], 5, -1444681467)),
|
||||
(p = f(p, m, n, o, a[d + 2], 9, -51403784)),
|
||||
(o = f(o, p, m, n, a[d + 7], 14, 1735328473)),
|
||||
(n = f(n, o, p, m, a[d + 12], 20, -1926607734)),
|
||||
(m = g(m, n, o, p, a[d + 5], 4, -378558)),
|
||||
(p = g(p, m, n, o, a[d + 8], 11, -2022574463)),
|
||||
(o = g(o, p, m, n, a[d + 11], 16, 1839030562)),
|
||||
(n = g(n, o, p, m, a[d + 14], 23, -35309556)),
|
||||
(m = g(m, n, o, p, a[d + 1], 4, -1530992060)),
|
||||
(p = g(p, m, n, o, a[d + 4], 11, 1272893353)),
|
||||
(o = g(o, p, m, n, a[d + 7], 16, -155497632)),
|
||||
(n = g(n, o, p, m, a[d + 10], 23, -1094730640)),
|
||||
(m = g(m, n, o, p, a[d + 13], 4, 681279174)),
|
||||
(p = g(p, m, n, o, a[d], 11, -358537222)),
|
||||
(o = g(o, p, m, n, a[d + 3], 16, -722521979)),
|
||||
(n = g(n, o, p, m, a[d + 6], 23, 76029189)),
|
||||
(m = g(m, n, o, p, a[d + 9], 4, -640364487)),
|
||||
(p = g(p, m, n, o, a[d + 12], 11, -421815835)),
|
||||
(o = g(o, p, m, n, a[d + 15], 16, 530742520)),
|
||||
(n = g(n, o, p, m, a[d + 2], 23, -995338651)),
|
||||
(m = h(m, n, o, p, a[d], 6, -198630844)),
|
||||
(p = h(p, m, n, o, a[d + 7], 10, 1126891415)),
|
||||
(o = h(o, p, m, n, a[d + 14], 15, -1416354905)),
|
||||
(n = h(n, o, p, m, a[d + 5], 21, -57434055)),
|
||||
(m = h(m, n, o, p, a[d + 12], 6, 1700485571)),
|
||||
(p = h(p, m, n, o, a[d + 3], 10, -1894986606)),
|
||||
(o = h(o, p, m, n, a[d + 10], 15, -1051523)),
|
||||
(n = h(n, o, p, m, a[d + 1], 21, -2054922799)),
|
||||
(m = h(m, n, o, p, a[d + 8], 6, 1873313359)),
|
||||
(p = h(p, m, n, o, a[d + 15], 10, -30611744)),
|
||||
(o = h(o, p, m, n, a[d + 6], 15, -1560198380)),
|
||||
(n = h(n, o, p, m, a[d + 13], 21, 1309151649)),
|
||||
(m = h(m, n, o, p, a[d + 4], 6, -145523070)),
|
||||
(p = h(p, m, n, o, a[d + 11], 10, -1120210379)),
|
||||
(o = h(o, p, m, n, a[d + 2], 15, 718787259)),
|
||||
(n = h(n, o, p, m, a[d + 9], 21, -343485551)),
|
||||
(m = b(m, i)),
|
||||
(n = b(n, j)),
|
||||
(o = b(o, k)),
|
||||
(p = b(p, l));
|
||||
return [m, n, o, p];
|
||||
}
|
||||
function j(a) {
|
||||
var b,
|
||||
c = "";
|
||||
for (b = 0; b < 32 * a.length; b += 8)
|
||||
c += String.fromCharCode((a[b >> 5] >>> b % 32) & 255);
|
||||
return c;
|
||||
}
|
||||
function k(a) {
|
||||
var b,
|
||||
c = [];
|
||||
for (c[(a.length >> 2) - 1] = void 0, b = 0; b < c.length; b += 1) c[b] = 0;
|
||||
for (b = 0; b < 8 * a.length; b += 8)
|
||||
c[b >> 5] |= (255 & a.charCodeAt(b / 8)) << b % 32;
|
||||
return c;
|
||||
}
|
||||
function l(a) {
|
||||
return j(i(k(a), 8 * a.length));
|
||||
}
|
||||
function m(a, b) {
|
||||
var c,
|
||||
d,
|
||||
e = k(a),
|
||||
f = [],
|
||||
g = [];
|
||||
for (
|
||||
f[15] = g[15] = void 0, e.length > 16 && (e = i(e, 8 * a.length)), c = 0;
|
||||
16 > c;
|
||||
c += 1
|
||||
)
|
||||
(f[c] = 909522486 ^ e[c]), (g[c] = 1549556828 ^ e[c]);
|
||||
return (d = i(f.concat(k(b)), 512 + 8 * b.length)), j(i(g.concat(d), 640));
|
||||
}
|
||||
function n(a) {
|
||||
var b,
|
||||
c,
|
||||
d = "0123456789abcdef",
|
||||
e = "";
|
||||
for (c = 0; c < a.length; c += 1)
|
||||
(b = a.charCodeAt(c)), (e += d.charAt((b >>> 4) & 15) + d.charAt(15 & b));
|
||||
return e;
|
||||
}
|
||||
function o(a) {
|
||||
return unescape(encodeURIComponent(a));
|
||||
}
|
||||
function p(a) {
|
||||
return l(o(a));
|
||||
}
|
||||
function q(a) {
|
||||
return n(p(a));
|
||||
}
|
||||
function r(a, b) {
|
||||
return m(o(a), o(b));
|
||||
}
|
||||
function s(a, b) {
|
||||
return n(r(a, b));
|
||||
}
|
||||
function t(a, b, c) {
|
||||
return b ? (c ? r(b, a) : s(b, a)) : c ? p(a) : q(a);
|
||||
}
|
||||
"function" == typeof define && define.amd
|
||||
? define(function () {
|
||||
return t;
|
||||
})
|
||||
: (a.md5 = t);
|
||||
})(this);
|
17
alist.go
17
alist.go
@ -1,26 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/bootstrap"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
_ "github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&conf.ConfigFile, "conf", "data/config.json", "config file")
|
||||
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
|
||||
flag.BoolVar(&conf.Version, "version", false, "print version info")
|
||||
flag.BoolVar(&conf.Password, "password", false, "print current password")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func Init() bool {
|
||||
bootstrap.InitLog()
|
||||
//bootstrap.InitLog()
|
||||
bootstrap.InitConf()
|
||||
bootstrap.InitCron()
|
||||
bootstrap.InitModel()
|
||||
@ -33,6 +25,7 @@ func Init() bool {
|
||||
log.Infof("current password: %s", pass.Value)
|
||||
return false
|
||||
}
|
||||
server.InitIndex()
|
||||
bootstrap.InitSettings()
|
||||
bootstrap.InitAccounts()
|
||||
bootstrap.InitCache()
|
||||
@ -55,8 +48,8 @@ func main() {
|
||||
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
|
||||
log.Infof("start server @ %s", base)
|
||||
var err error
|
||||
if conf.Conf.Https {
|
||||
err = r.RunTLS(base, conf.Conf.CertFile, conf.Conf.KeyFile)
|
||||
if conf.Conf.Scheme.Https {
|
||||
err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
|
||||
} else {
|
||||
err = r.Run(base)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -15,7 +15,7 @@ func InitAccounts() {
|
||||
}
|
||||
for i, account := range accounts {
|
||||
model.RegisterAccount(account)
|
||||
driver, ok := drivers.GetDriver(account.Type)
|
||||
driver, ok := base.GetDriver(account.Type)
|
||||
if !ok {
|
||||
log.Errorf("no [%s] driver", account.Type)
|
||||
} else {
|
||||
|
@ -12,7 +12,11 @@ import (
|
||||
// InitCache init cache
|
||||
func InitCache() {
|
||||
log.Infof("init cache...")
|
||||
goCacheClient := goCache.New(60*time.Minute, 120*time.Minute)
|
||||
c := conf.Conf.Cache
|
||||
if c.Expiration == 0 {
|
||||
c.Expiration, c.CleanupInterval = 60, 120
|
||||
}
|
||||
goCacheClient := goCache.New(time.Duration(c.Expiration)*time.Minute, time.Duration(c.CleanupInterval)*time.Minute)
|
||||
goCacheStore := store.NewGoCache(goCacheClient, nil)
|
||||
conf.Cache = cache.New(goCacheStore)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -19,4 +20,13 @@ func InitLog() {
|
||||
FullTimestamp: true,
|
||||
})
|
||||
log.Infof("init log...")
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&conf.ConfigFile, "conf", "data/config.json", "config file")
|
||||
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
|
||||
flag.BoolVar(&conf.Version, "version", false, "print version info")
|
||||
flag.BoolVar(&conf.Password, "password", false, "print current password")
|
||||
flag.Parse()
|
||||
InitLog()
|
||||
}
|
@ -5,19 +5,16 @@ import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func InitSettings() {
|
||||
log.Infof("init settings...")
|
||||
version := model.SettingItem{
|
||||
Key: "version",
|
||||
Value: conf.GitTag,
|
||||
Description: "version",
|
||||
Type: "string",
|
||||
Group: model.CONST,
|
||||
}
|
||||
|
||||
_ = model.SaveSetting(version)
|
||||
err := model.SaveSetting(model.Version)
|
||||
if err != nil {
|
||||
log.Fatalf("failed write setting: %s", err.Error())
|
||||
}
|
||||
|
||||
settings := []model.SettingItem{
|
||||
{
|
||||
@ -25,140 +22,197 @@ func InitSettings() {
|
||||
Value: "Alist",
|
||||
Description: "title",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "password",
|
||||
Value: "alist",
|
||||
Description: "password",
|
||||
Type: "string",
|
||||
Group: model.PRIVATE,
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "logo",
|
||||
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
||||
Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png",
|
||||
Description: "logo",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "favicon",
|
||||
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
||||
Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png",
|
||||
Description: "favicon",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "icon color",
|
||||
Value: "teal.300",
|
||||
Value: "#1890ff",
|
||||
Description: "icon's color",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "text types",
|
||||
Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx",
|
||||
Value: strings.Join(conf.TextTypes, ","),
|
||||
Type: "string",
|
||||
Description: "text type extensions",
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "hide readme file",
|
||||
Value: "true",
|
||||
Type: "bool",
|
||||
Description: "hide readme file? ",
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "music cover",
|
||||
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
||||
Description: "music cover image",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "site beian",
|
||||
Description: "chinese beian info",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "home readme url",
|
||||
Description: "when have multiple, the readme file to show",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "markdown theme",
|
||||
Value: "vuepress",
|
||||
Description: "default | github | vuepress",
|
||||
Group: model.PUBLIC,
|
||||
Type: "select",
|
||||
Values: "default,github,vuepress",
|
||||
Key: "autoplay video",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "autoplay video",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Group: model.PUBLIC,
|
||||
},
|
||||
{
|
||||
Key: "autoplay audio",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Group: model.PUBLIC,
|
||||
Key: "autoplay audio",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "check parent folder",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Description: "check parent folder password",
|
||||
Group: model.PRIVATE,
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "customize style",
|
||||
Key: "customize head",
|
||||
Value: "",
|
||||
Type: "text",
|
||||
Description: "customize style, don't need add <style></style>",
|
||||
Group: model.PRIVATE,
|
||||
Description: "Customize head, placed at the beginning of the head",
|
||||
Access: model.PRIVATE,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "customize script",
|
||||
Key: "customize body",
|
||||
Value: "",
|
||||
Type: "text",
|
||||
Description: "customize script, don't need add <script></script>",
|
||||
Group: model.PRIVATE,
|
||||
Description: "Customize script, placed at the end of the body",
|
||||
Access: model.PRIVATE,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "animation",
|
||||
Value: "true",
|
||||
Type: "bool",
|
||||
Description: "when there are a lot of files, the animation will freeze when opening",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "check down link",
|
||||
Value: "false",
|
||||
Type: "bool",
|
||||
Description: "check down link password, your link will be 'https://alist.com/d/filename?pw=xxx'",
|
||||
Group: model.PUBLIC,
|
||||
Access: model.PUBLIC,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "WebDAV username",
|
||||
Value: "alist",
|
||||
Value: "alist_admin",
|
||||
Description: "WebDAV username",
|
||||
Type: "string",
|
||||
Group: model.PRIVATE,
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "WebDAV password",
|
||||
Value: "alist",
|
||||
Value: "alist_admin",
|
||||
Description: "WebDAV password",
|
||||
Type: "string",
|
||||
Group: model.PRIVATE,
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "artplayer whitelist",
|
||||
Value: "*",
|
||||
Description: "refer to https://artplayer.org/document/options#whitelist",
|
||||
Type: "string",
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "artplayer autoSize",
|
||||
Value: "true",
|
||||
Description: "refer to https://artplayer.org/document/options#autosize",
|
||||
Type: "bool",
|
||||
Access: model.PUBLIC,
|
||||
Group: model.FRONT,
|
||||
},
|
||||
{
|
||||
Key: "Visitor WebDAV username",
|
||||
Value: "alist_visitor",
|
||||
Description: "Visitor WebDAV username",
|
||||
Type: "string",
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
{
|
||||
Key: "Visitor WebDAV password",
|
||||
Value: "alist_visitor",
|
||||
Description: "Visitor WebDAV password",
|
||||
Type: "string",
|
||||
Access: model.PRIVATE,
|
||||
Group: model.BACK,
|
||||
},
|
||||
}
|
||||
for _, v := range settings {
|
||||
_, err := model.GetSettingByKey(v.Key)
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
for i, _ := range settings {
|
||||
v := settings[i]
|
||||
v.Version = conf.GitTag
|
||||
o, err := model.GetSettingByKey(v.Key)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
err = model.SaveSetting(v)
|
||||
if err != nil {
|
||||
log.Fatalf("failed write setting: %s", err.Error())
|
||||
}
|
||||
} else {
|
||||
log.Fatal("can't get setting: %s", err.Error())
|
||||
}
|
||||
} else {
|
||||
//o.Version = conf.GitTag
|
||||
//err = model.SaveSetting(*o)
|
||||
v.Value = o.Value
|
||||
err = model.SaveSetting(v)
|
||||
if err != nil {
|
||||
log.Fatalf("failed write setting: %s", err.Error())
|
||||
|
8
build.sh
8
build.sh
@ -37,8 +37,14 @@ yarn
|
||||
if [ "$1" == "release" ]; then
|
||||
yarn build --base="https://cdn.jsdelivr.net/gh/Xhofe/alist-web@cdn/v2/$webCommit"
|
||||
mv dist/assets ..
|
||||
mv dist/index.html ../alist/public
|
||||
# 构建local
|
||||
yarn build
|
||||
mv dist/index.html dist/local.html
|
||||
mv dist/* ../alist/public
|
||||
else
|
||||
yarn build
|
||||
mv dist/* ../alist/public
|
||||
fi
|
||||
cd ..
|
||||
|
||||
@ -61,8 +67,6 @@ ldflags="\
|
||||
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
|
||||
"
|
||||
|
||||
cp -R ../alist-web/dist/* public
|
||||
|
||||
if [ "$1" == "release" ]; then
|
||||
xgo -out alist -ldflags="$ldflags" .
|
||||
else
|
||||
|
@ -10,13 +10,25 @@ type Database struct {
|
||||
TablePrefix string `json:"table_prefix"`
|
||||
DBFile string `json:"db_file"`
|
||||
}
|
||||
|
||||
type Scheme struct {
|
||||
Https bool `json:"https"`
|
||||
CertFile string `json:"cert_file"`
|
||||
KeyFile string `json:"key_file"`
|
||||
}
|
||||
|
||||
type CacheConfig struct {
|
||||
Expiration int64 `json:"expiration"`
|
||||
CleanupInterval int64 `json:"cleanup_interval"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Database Database `json:"database"`
|
||||
Https bool `json:"https"`
|
||||
CertFile string `json:"cert_file"`
|
||||
KeyFile string `json:"key_file"`
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Local bool `json:"local"`
|
||||
Database Database `json:"database"`
|
||||
Scheme Scheme `json:"scheme"`
|
||||
Cache CacheConfig `json:"cache"`
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
@ -29,5 +41,9 @@ func DefaultConfig() *Config {
|
||||
TablePrefix: "x_",
|
||||
DBFile: "data/data.db",
|
||||
},
|
||||
Cache: CacheConfig{
|
||||
Expiration: 60,
|
||||
CleanupInterval: 120,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
24
conf/var.go
24
conf/var.go
@ -12,7 +12,7 @@ var (
|
||||
GoVersion string
|
||||
GitAuthor string
|
||||
GitCommit string
|
||||
GitTag string
|
||||
GitTag string = "dev"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -29,11 +29,13 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
TextTypes = []string{"txt", "go", "md"}
|
||||
TextTypes = []string{"txt", "htm", "html", "xml", "java", "properties", "sql",
|
||||
"js", "md", "json", "conf", "ini", "vue", "php", "py", "bat", "gitignore", "yml",
|
||||
"go", "sh", "c", "cpp", "h", "hpp", "tsx", "vtt", "srt", "ass"}
|
||||
OfficeTypes = []string{"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"}
|
||||
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb"}
|
||||
AudioTypes = []string{"mp3", "flac", "ogg", "m4a"}
|
||||
ImageTypes = []string{"jpg", "tiff", "jpeg", "png", "gif", "bmp", "svg"}
|
||||
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb", "webm"}
|
||||
AudioTypes = []string{"mp3", "flac", "ogg", "m4a", "wav"}
|
||||
ImageTypes = []string{"jpg", "tiff", "jpeg", "png", "gif", "bmp", "svg", "ico"}
|
||||
)
|
||||
|
||||
// settings
|
||||
@ -41,11 +43,11 @@ var (
|
||||
RawIndexHtml string
|
||||
IndexHtml string
|
||||
CheckParent bool
|
||||
//CustomizeStyle string
|
||||
//CustomizeScript string
|
||||
//Favicon string
|
||||
CheckDown bool
|
||||
CheckDown bool
|
||||
|
||||
DavUsername string
|
||||
DavPassword string
|
||||
Token string
|
||||
DavUsername string
|
||||
DavPassword string
|
||||
VisitorDavUsername string
|
||||
VisitorDavPassword string
|
||||
)
|
||||
|
148
drivers/123.go
148
drivers/123.go
@ -1,148 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pan123Client = resty.New()
|
||||
|
||||
type Pan123TokenResp struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Pan123File struct {
|
||||
FileName string `json:"FileName"`
|
||||
Size int64 `json:"Size"`
|
||||
UpdateAt *time.Time `json:"UpdateAt"`
|
||||
FileId int64 `json:"FileId"`
|
||||
Type int `json:"Type"`
|
||||
Etag string `json:"Etag"`
|
||||
S3KeyFlag string `json:"S3KeyFlag"`
|
||||
}
|
||||
|
||||
type Pan123Files struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
InfoList []Pan123File `json:"InfoList"`
|
||||
Next string `json:"Next"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type Pan123DownResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
DownloadUrl string `json:"DownloadUrl"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (driver Pan123) Login(account *model.Account) error {
|
||||
var resp Pan123TokenResp
|
||||
_, err := pan123Client.R().
|
||||
SetResult(&resp).
|
||||
SetBody(Json{
|
||||
"passport": account.Username,
|
||||
"password": account.Password,
|
||||
}).Post("https://www.123pan.com/api/user/sign_in")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Code != 200 {
|
||||
err = fmt.Errorf(resp.Message)
|
||||
account.Status = resp.Message
|
||||
} else {
|
||||
account.Status = "work"
|
||||
account.AccessToken = resp.Data.Token
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) FormatFile(file *Pan123File) *model.File {
|
||||
f := &model.File{
|
||||
Id: strconv.FormatInt(file.FileId, 10),
|
||||
Name: file.FileName,
|
||||
Size: file.Size,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: file.UpdateAt,
|
||||
}
|
||||
if file.Type == 1 {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
f.Type = utils.GetFileType(filepath.Ext(file.FileName))
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File, error) {
|
||||
next := "0"
|
||||
res := make([]Pan123File, 0)
|
||||
for next != "-1" {
|
||||
var resp Pan123Files
|
||||
_, err := pan123Client.R().SetResult(&resp).
|
||||
SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
SetQueryParams(map[string]string{
|
||||
"driveId": "0",
|
||||
"limit": "100",
|
||||
"next": next,
|
||||
"orderBy": account.OrderBy,
|
||||
"orderDirection": account.OrderDirection,
|
||||
"parentFileId": parentId,
|
||||
"trashed": "false",
|
||||
}).Get("https://www.123pan.com/api/file/list")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
if resp.Code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.GetFiles(parentId, account)
|
||||
}
|
||||
return nil, fmt.Errorf(resp.Message)
|
||||
}
|
||||
next = resp.Data.Next
|
||||
res = append(res, resp.Data.InfoList...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
||||
dir, name := filepath.Split(path)
|
||||
dir = utils.ParsePath(dir)
|
||||
_, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
||||
parentFiles, _ := parentFiles_.([]Pan123File)
|
||||
for _, file := range parentFiles {
|
||||
if file.FileName == name {
|
||||
if file.Type != conf.FOLDER {
|
||||
return &file, err
|
||||
} else {
|
||||
return nil, NotFile
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&Pan123{})
|
||||
pan123Client.SetRetryCount(3)
|
||||
}
|
232
drivers/123/123.go
Normal file
232
drivers/123/123.go
Normal file
@ -0,0 +1,232 @@
|
||||
package _23
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"math/rand"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaseResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Pan123TokenResp struct {
|
||||
BaseResp
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type Pan123File struct {
|
||||
FileName string `json:"FileName"`
|
||||
Size int64 `json:"Size"`
|
||||
UpdateAt *time.Time `json:"UpdateAt"`
|
||||
FileId int64 `json:"FileId"`
|
||||
Type int `json:"Type"`
|
||||
Etag string `json:"Etag"`
|
||||
S3KeyFlag string `json:"S3KeyFlag"`
|
||||
}
|
||||
|
||||
type Pan123Files struct {
|
||||
BaseResp
|
||||
Data struct {
|
||||
InfoList []Pan123File `json:"InfoList"`
|
||||
Next string `json:"Next"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type Pan123DownResp struct {
|
||||
BaseResp
|
||||
Data struct {
|
||||
DownloadUrl string `json:"DownloadUrl"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (driver Pan123) Login(account *model.Account) error {
|
||||
url := "https://www.123pan.com/api/user/sign_in"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var resp Pan123TokenResp
|
||||
_, err := base.RestyClient.R().
|
||||
SetResult(&resp).
|
||||
SetBody(base.Json{
|
||||
"passport": account.Username,
|
||||
"password": account.Password,
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Code != 200 {
|
||||
err = fmt.Errorf(resp.Message)
|
||||
account.Status = resp.Message
|
||||
} else {
|
||||
account.Status = "work"
|
||||
account.AccessToken = resp.Data.Token
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) FormatFile(file *Pan123File) *model.File {
|
||||
f := &model.File{
|
||||
Id: strconv.FormatInt(file.FileId, 10),
|
||||
Name: file.FileName,
|
||||
Size: file.Size,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: file.UpdateAt,
|
||||
}
|
||||
if file.Type == 1 {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
f.Type = utils.GetFileType(filepath.Ext(file.FileName))
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File, error) {
|
||||
next := "0"
|
||||
res := make([]Pan123File, 0)
|
||||
for next != "-1" {
|
||||
var resp Pan123Files
|
||||
query := map[string]string{
|
||||
"driveId": "0",
|
||||
"limit": "100",
|
||||
"next": next,
|
||||
"orderBy": account.OrderBy,
|
||||
"orderDirection": account.OrderDirection,
|
||||
"parentFileId": parentId,
|
||||
"trashed": "false",
|
||||
}
|
||||
_, err := driver.Request("https://www.123pan.com/api/file/list",
|
||||
base.Get, nil, query, nil, &resp, false, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
next = resp.Data.Next
|
||||
res = append(res, resp.Data.InfoList...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Request(url string, method int, headers, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" && proxy {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var res *resty.Response
|
||||
var err error
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
body := res.Body()
|
||||
code := jsoniter.Get(body, "code").ToInt()
|
||||
if code != 0 {
|
||||
if code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, headers, query, data, resp, proxy, account)
|
||||
}
|
||||
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
//func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
||||
// res, err := pan123Client.R().
|
||||
// SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
// SetBody(data).Post(url)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// body := res.Body()
|
||||
// if jsoniter.Get(body, "code").ToInt() != 0 {
|
||||
// return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||
// }
|
||||
// return body, nil
|
||||
//}
|
||||
|
||||
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
||||
dir, name := filepath.Split(path)
|
||||
dir = utils.ParsePath(dir)
|
||||
_, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parentFiles_, _ := base.GetCache(dir, account)
|
||||
parentFiles, _ := parentFiles_.([]Pan123File)
|
||||
for _, file := range parentFiles {
|
||||
if file.FileName == name {
|
||||
if file.Type != conf.FOLDER {
|
||||
return &file, err
|
||||
} else {
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func RandStr(length int) string {
|
||||
str := "123456789abcdefghijklmnopqrstuvwxyz"
|
||||
bytes := []byte(str)
|
||||
var result []byte
|
||||
rand.Seed(time.Now().UnixNano() + int64(rand.Intn(100)))
|
||||
for i := 0; i < length; i++ {
|
||||
result = append(result, bytes[rand.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func HMAC(message string, secret string) string {
|
||||
key := []byte(secret)
|
||||
h := hmac.New(sha256.New, key)
|
||||
h.Write([]byte(message))
|
||||
// fmt.Println(h.Sum(nil))
|
||||
//sha := hex.EncodeToString(h.Sum(nil))
|
||||
// fmt.Println(sha)
|
||||
//return sha
|
||||
return string(h.Sum(nil))
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Pan123{})
|
||||
}
|
355
drivers/123/driver.go
Normal file
355
drivers/123/driver.go
Normal file
@ -0,0 +1,355 @@
|
||||
package _23
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pan123 struct{}
|
||||
|
||||
func (driver Pan123) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "123Pan",
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Pan123) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account username/phone number",
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account password",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: base.TypeSelect,
|
||||
Values: "name,fileId,updateAt,createAt",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: base.TypeSelect,
|
||||
Values: "asc,desc",
|
||||
Required: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Pan123) Save(account *model.Account, old *model.Account) error {
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "0"
|
||||
}
|
||||
err := driver.Login(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []Pan123File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]Pan123File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = base.SetCache(path, rawFiles, account)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
log.Debugf("%+v", args)
|
||||
file, err := driver.GetFile(utils.ParsePath(args.Path), account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp Pan123DownResp
|
||||
var headers map[string]string
|
||||
if args.IP != "" && args.IP != "::1" {
|
||||
headers = map[string]string{
|
||||
//"X-Real-IP": "1.1.1.1",
|
||||
"X-Forwarded-For": args.IP,
|
||||
}
|
||||
}
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"etag": file.Etag,
|
||||
"fileId": file.FileId,
|
||||
"fileName": file.FileName,
|
||||
"s3keyFlag": file.S3KeyFlag,
|
||||
"size": file.Size,
|
||||
"type": file.Type,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/download_info",
|
||||
base.Post, headers, nil, &data, &resp, false, account)
|
||||
//_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
// SetBody().Post("https://www.123pan.com/api/file/download_info")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := url.Parse(resp.Data.DownloadUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u_ := fmt.Sprintf("https://%s%s", u.Host, u.Path)
|
||||
res, err := base.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
link := base.Link{
|
||||
Url: resp.Data.DownloadUrl,
|
||||
}
|
||||
if res.StatusCode() == 302 {
|
||||
link.Url = res.Header().Get("location")
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("pan123 path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Del("origin")
|
||||
}
|
||||
|
||||
func (driver Pan123) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Pan123) MakeDir(path string, account *model.Account) error {
|
||||
dir, name := filepath.Split(path)
|
||||
parentFile, err := driver.File(dir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
parentFileId, _ := strconv.Atoi(parentFile.Id)
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"etag": "",
|
||||
"fileName": name,
|
||||
"parentFileId": parentFileId,
|
||||
"size": 0,
|
||||
"type": 1,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) Move(src string, dst string, account *model.Account) error {
|
||||
srcDir, _ := filepath.Split(src)
|
||||
dstDir, dstName := filepath.Split(dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileId, _ := strconv.Atoi(srcFile.Id)
|
||||
// rename
|
||||
if srcDir == dstDir {
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"fileId": fileId,
|
||||
"fileName": dstName,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/rename",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parentFileId, _ := strconv.Atoi(dstDirFile.Id)
|
||||
data := base.Json{
|
||||
"fileId": fileId,
|
||||
"parentFileId": parentFileId,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/mod_pid",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
|
||||
}
|
||||
if err != nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Pan123) Delete(path string, account *model.Account) error {
|
||||
file, err := driver.GetFile(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"operation": true,
|
||||
"fileTrashInfoList": file,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/trash",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type UploadResp struct {
|
||||
XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
|
||||
Bucket string `xml:"Bucket"`
|
||||
Key string `xml:"Key"`
|
||||
UploadId string `xml:"UploadId"`
|
||||
}
|
||||
|
||||
// TODO unfinished
|
||||
func (driver Pan123) Upload(file *model.FileStream, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
parentFileId, _ := strconv.Atoi(parentFile.Id)
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"duplicate": true,
|
||||
"etag": RandStr(32), //maybe file's md5
|
||||
"fileName": file.GetFileName(),
|
||||
"parentFileId": parentFileId,
|
||||
"size": file.GetSize(),
|
||||
"type": 0,
|
||||
}
|
||||
res, err := driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
baseUrl := fmt.Sprintf("https://file.123pan.com/%s/%s", jsoniter.Get(res, "data.Bucket").ToString(), jsoniter.Get(res, "data.Key").ToString())
|
||||
var resp UploadResp
|
||||
kSecret := jsoniter.Get(res, "data.SecretAccessKey").ToString()
|
||||
nowTimeStr := time.Now().String()
|
||||
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0], "-", "")
|
||||
|
||||
StringToSign := fmt.Sprintf("%s\n%s\n%s\n%s",
|
||||
"AWS4-HMAC-SHA256",
|
||||
nowTimeStr,
|
||||
fmt.Sprintf("%s/us-east-1/s3/aws4_request", Date),
|
||||
)
|
||||
|
||||
kDate := HMAC("AWS4"+kSecret, Date)
|
||||
kRegion := HMAC(kDate, "us-east-1")
|
||||
kService := HMAC(kRegion, "s3")
|
||||
kSigning := HMAC(kService, "aws4_request")
|
||||
_, err = base.RestyClient.R().SetResult(&resp).SetHeaders(map[string]string{
|
||||
"Authorization": fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=%s",
|
||||
jsoniter.Get(res, "data.AccessKeyId"),
|
||||
Date,
|
||||
hex.EncodeToString([]byte(HMAC(StringToSign, kSigning)))),
|
||||
"X-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
|
||||
"X-Amz-Date": nowTimeStr,
|
||||
"x-amz-security-token": jsoniter.Get(res, "data.SessionToken").ToString(),
|
||||
}).Post(fmt.Sprintf("%s?uploads", baseUrl))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Pan123)(nil)
|
@ -1,192 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
url "net/url"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Pan123 struct {}
|
||||
|
||||
func (driver Pan123) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "123Pan",
|
||||
OnlyProxy: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Pan123) Items() []Item {
|
||||
return []Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "account username/phone number",
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "account password",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: "string",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Values: "name,fileId,updateAt,createAt",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: "select",
|
||||
Values: "asc,desc",
|
||||
Required: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Pan123) Save(account *model.Account, old *model.Account) error {
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "0"
|
||||
}
|
||||
err := driver.Login(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Pan123) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
}
|
||||
|
||||
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []Pan123File
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]Pan123File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.GetFile(utils.ParsePath(path), account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var resp Pan123DownResp
|
||||
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
SetBody(Json{
|
||||
"driveId": 0,
|
||||
"etag": file.Etag,
|
||||
"fileId": file.FileId,
|
||||
"fileName": file.FileName,
|
||||
"s3keyFlag": file.S3KeyFlag,
|
||||
"size": file.Size,
|
||||
"type": file.Type,
|
||||
}).Post("https://www.123pan.com/api/file/download_info")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
if resp.Code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
return "", fmt.Errorf(resp.Message)
|
||||
}
|
||||
u,err := url.Parse(resp.Data.DownloadUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u_ := fmt.Sprintf("https://%s%s",u.Host,u.Path)
|
||||
res, err := NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if res.StatusCode() == 302 {
|
||||
return res.Header().Get("location"), nil
|
||||
}
|
||||
return resp.Data.DownloadUrl, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("pan123 path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Del("origin")
|
||||
}
|
||||
|
||||
func (driver Pan123) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*Pan123)(nil)
|
@ -1,19 +1,28 @@
|
||||
package drivers
|
||||
package _89
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha1"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
mathRand "math/rand"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -21,7 +30,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
var client189Map map[string]*resty.Client
|
||||
|
||||
func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
|
||||
@ -51,7 +59,7 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
|
||||
//func (c Cloud189) GetFile(path string, account *model.Account) (*Cloud189File, error) {
|
||||
// dir, name := filepath.Split(path)
|
||||
// dir = utils.ParsePath(dir)
|
||||
// _, _, err := c.Path(dir, account)
|
||||
// _, _, err := c.ParentPath(dir, account)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
@ -62,11 +70,11 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
|
||||
// if file.Size != -1 {
|
||||
// return &file, err
|
||||
// } else {
|
||||
// return nil, NotFile
|
||||
// return nil, ErrNotFile
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil, PathNotFound
|
||||
// return nil, ErrPathNotFound
|
||||
//}
|
||||
|
||||
type Cloud189Down struct {
|
||||
@ -89,6 +97,7 @@ func (driver Cloud189) Login(account *model.Account) error {
|
||||
client = resty.New()
|
||||
//client.SetCookieJar(cookieJar)
|
||||
client.SetRetryCount(3)
|
||||
client.SetHeader("Referer", "https://cloud.189.cn/")
|
||||
}
|
||||
url := "https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https%3A%2F%2Fcloud.189.cn%2Fmain.action"
|
||||
b := ""
|
||||
@ -239,7 +248,6 @@ func (driver Cloud189) GetFiles(fileId string, account *model.Account) ([]Cloud1
|
||||
if resp.FileListAO.Count == 0 {
|
||||
break
|
||||
}
|
||||
res = append(res, resp.FileListAO.FileList...)
|
||||
for _, folder := range resp.FileListAO.FolderList {
|
||||
res = append(res, Cloud189File{
|
||||
Id: folder.Id,
|
||||
@ -248,11 +256,108 @@ func (driver Cloud189) GetFiles(fileId string, account *model.Account) ([]Cloud1
|
||||
Size: -1,
|
||||
})
|
||||
}
|
||||
res = append(res, resp.FileListAO.FileList...)
|
||||
pageNum++
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Request(url string, method string, form map[string]string, headers map[string]string, account *model.Account) ([]byte, error) {
|
||||
client, ok := client189Map[account.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can't find [%s] client", account.Name)
|
||||
}
|
||||
//var resp base.Json
|
||||
var e Cloud189Error
|
||||
req := client.R().SetError(&e).
|
||||
SetHeader("Accept", "application/json;charset=UTF-8").
|
||||
SetQueryParams(map[string]string{
|
||||
"noCache": random(),
|
||||
})
|
||||
if form != nil {
|
||||
req = req.SetFormData(form)
|
||||
}
|
||||
if headers != nil {
|
||||
req = req.SetHeaders(headers)
|
||||
}
|
||||
var err error
|
||||
var res *resty.Response
|
||||
if strings.ToUpper(method) == "GET" {
|
||||
res, err = req.Get(url)
|
||||
} else {
|
||||
res, err = req.Post(url)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.ErrorCode != "" {
|
||||
if e.ErrorCode == "InvalidSessionKey" {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(url, method, form, nil, account)
|
||||
}
|
||||
}
|
||||
//log.Debug(res, jsoniter.Get(res.Body(),"res_code").ToInt())
|
||||
if jsoniter.Get(res.Body(), "res_code").ToInt() != 0 {
|
||||
err = errors.New(jsoniter.Get(res.Body(), "res_message").ToString())
|
||||
}
|
||||
return res.Body(), err
|
||||
}
|
||||
|
||||
func (driver Cloud189) GetSessionKey(account *model.Account) (string, error) {
|
||||
resp, err := driver.Request("https://cloud.189.cn/v2/getUserBriefInfo.action", "GET", nil, nil, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return jsoniter.Get(resp, "sessionKey").ToString(), nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) GetResKey(account *model.Account) (string, string, error) {
|
||||
resp, err := driver.Request("https://cloud.189.cn/api/security/generateRsaKey.action", "GET", nil, nil, account)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return jsoniter.Get(resp, "pubKey").ToString(), jsoniter.Get(resp, "pkId").ToString(), nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) UploadRequest(url string, form map[string]string, account *model.Account) ([]byte, error) {
|
||||
sessionKey, err := driver.GetSessionKey(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pubKey, pkId, err := driver.GetResKey(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
xRId := "e007e99a-370c-4a14-a143-1b1541972fcf"
|
||||
pkey := strings.ReplaceAll(xRId, "-", "")
|
||||
params := aesEncrypt(qs(form), pkey[:16])
|
||||
date := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
signature := hmacSha1(fmt.Sprintf("SessionKey=%s&Operate=GET&RequestURI=%s&Date=%s¶ms=%s", sessionKey, url, date, params), pkey)
|
||||
encryptionText := RsaEncode([]byte(pkey), pubKey)
|
||||
res, err := base.RestyClient.R().SetHeaders(map[string]string{
|
||||
"signature": signature,
|
||||
"sessionKey": sessionKey,
|
||||
"encryptionText": encryptionText,
|
||||
"pkId": pkId,
|
||||
"x-request-id": xRId,
|
||||
"x-request-date": date,
|
||||
"origin": "https://cloud.189.cn",
|
||||
"referer": "https://cloud.189.cn/",
|
||||
}).SetQueryParam("params", params).Get("https://upload.cloud.189.cn" + url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
data := res.Body()
|
||||
if jsoniter.Get(data, "code").ToString() != "SUCCESS" {
|
||||
return nil, errors.New(jsoniter.Get(data, "msg").ToString())
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func random() string {
|
||||
return fmt.Sprintf("0.%17v", mathRand.New(mathRand.NewSource(time.Now().UnixNano())).Int63n(100000000000000000))
|
||||
}
|
||||
@ -311,7 +416,76 @@ func b64tohex(a string) string {
|
||||
return d
|
||||
}
|
||||
|
||||
func qs(form map[string]string) string {
|
||||
strList := make([]string, 0)
|
||||
for k, v := range form {
|
||||
strList = append(strList, fmt.Sprintf("%s=%s", k, url.QueryEscape(v)))
|
||||
}
|
||||
return strings.Join(strList, "&")
|
||||
}
|
||||
|
||||
func aesEncrypt(data, key string) string {
|
||||
encrypted := AesEncryptECB([]byte(data), []byte(key))
|
||||
//return string(encrypted)
|
||||
return hex.EncodeToString(encrypted)
|
||||
}
|
||||
|
||||
func hmacSha1(data string, secret string) string {
|
||||
h := hmac.New(sha1.New, []byte(secret))
|
||||
h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func AesEncryptECB(origData []byte, key []byte) (encrypted []byte) {
|
||||
cipher, _ := aes.NewCipher(generateKey(key))
|
||||
length := (len(origData) + aes.BlockSize) / aes.BlockSize
|
||||
plain := make([]byte, length*aes.BlockSize)
|
||||
copy(plain, origData)
|
||||
pad := byte(len(plain) - len(origData))
|
||||
for i := len(origData); i < len(plain); i++ {
|
||||
plain[i] = pad
|
||||
}
|
||||
encrypted = make([]byte, len(plain))
|
||||
// 分组分块加密
|
||||
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
||||
}
|
||||
|
||||
return encrypted
|
||||
}
|
||||
func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
|
||||
cipher, _ := aes.NewCipher(generateKey(key))
|
||||
decrypted = make([]byte, len(encrypted))
|
||||
//
|
||||
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
||||
}
|
||||
|
||||
trim := 0
|
||||
if len(decrypted) > 0 {
|
||||
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
|
||||
}
|
||||
|
||||
return decrypted[:trim]
|
||||
}
|
||||
func generateKey(key []byte) (genKey []byte) {
|
||||
genKey = make([]byte, 16)
|
||||
copy(genKey, key)
|
||||
for i := 16; i < len(key); {
|
||||
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
|
||||
genKey[j] ^= key[i]
|
||||
}
|
||||
}
|
||||
return genKey
|
||||
}
|
||||
|
||||
func getMd5(data []byte) []byte {
|
||||
h := md5.New()
|
||||
h.Write(data)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&Cloud189{})
|
||||
base.RegisterDriver(&Cloud189{})
|
||||
client189Map = make(map[string]*resty.Client, 0)
|
||||
}
|
||||
}
|
442
drivers/189/driver.go
Normal file
442
drivers/189/driver.go
Normal file
@ -0,0 +1,442 @@
|
||||
package _89
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Cloud189 struct{}
|
||||
|
||||
func (driver Cloud189) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "189Cloud",
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Cloud189) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account username/phone number",
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account password",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: base.TypeSelect,
|
||||
Values: "name,size,lastOpTime,createdDate",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "desc",
|
||||
Type: base.TypeSelect,
|
||||
Values: "true,false",
|
||||
Required: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Cloud189) Save(account *model.Account, old *model.Account) error {
|
||||
if old != nil && old.Name != account.Name {
|
||||
delete(client189Map, old.Name)
|
||||
}
|
||||
if err := driver.Login(account); err != nil {
|
||||
account.Status = err.Error()
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
account.Status = "work"
|
||||
err := model.SaveAccount(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []Cloud189File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]Cloud189File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = base.SetCache(path, rawFiles, account)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(utils.ParsePath(args.Path), account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file.Type == conf.FOLDER {
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
client, ok := client189Map[account.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can't find [%s] client", account.Name)
|
||||
}
|
||||
var e Cloud189Error
|
||||
var resp Cloud189Down
|
||||
_, err = client.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("Accept", "application/json;charset=UTF-8").
|
||||
SetQueryParams(map[string]string{
|
||||
"noCache": random(),
|
||||
"fileId": file.Id,
|
||||
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.ErrorCode != "" {
|
||||
if e.ErrorCode == "InvalidSessionKey" {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Link(args, account)
|
||||
}
|
||||
}
|
||||
if resp.ResCode != 0 {
|
||||
return nil, fmt.Errorf(resp.ResMessage)
|
||||
}
|
||||
res, err := base.NoRedirectClient.R().Get(resp.FileDownloadUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link := base.Link{}
|
||||
if res.StatusCode() == 302 {
|
||||
link.Url = res.Header().Get("location")
|
||||
} else {
|
||||
link.Url = resp.FileDownloadUrl
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("189 path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Proxy(ctx *gin.Context, account *model.Account) {
|
||||
ctx.Request.Header.Del("Origin")
|
||||
}
|
||||
|
||||
func (driver Cloud189) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Cloud189) MakeDir(path string, account *model.Account) error {
|
||||
dir, name := filepath.Split(path)
|
||||
parent, err := driver.File(dir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parent.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
form := map[string]string{
|
||||
"parentFolderId": parent.Id,
|
||||
"folderName": name,
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Cloud189) Move(src string, dst string, account *model.Account) error {
|
||||
srcDir, _ := filepath.Split(src)
|
||||
dstDir, dstName := filepath.Split(dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// rename
|
||||
if srcDir == dstDir {
|
||||
url := "https://cloud.189.cn/api/open/file/renameFile.action"
|
||||
idKey := "fileId"
|
||||
nameKey := "destFileName"
|
||||
if srcFile.IsDir() {
|
||||
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
|
||||
idKey = "folderId"
|
||||
nameKey = "destFolderName"
|
||||
}
|
||||
form := map[string]string{
|
||||
idKey: srcFile.Id,
|
||||
nameKey: dstName,
|
||||
}
|
||||
_, err = driver.Request(url, "POST", form, nil, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
isFolder := 0
|
||||
if srcFile.IsDir() {
|
||||
isFolder = 1
|
||||
}
|
||||
taskInfos := []base.Json{
|
||||
{
|
||||
"fileId": srcFile.Id,
|
||||
"fileName": dstName,
|
||||
"isFolder": isFolder,
|
||||
},
|
||||
}
|
||||
taskInfosBytes, err := json.Marshal(taskInfos)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
form := map[string]string{
|
||||
"type": "MOVE",
|
||||
"targetFolderId": dstDirFile.Id,
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Cloud189) Copy(src string, dst string, account *model.Account) error {
|
||||
dstDir, dstName := filepath.Split(dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
isFolder := 0
|
||||
if srcFile.IsDir() {
|
||||
isFolder = 1
|
||||
}
|
||||
taskInfos := []base.Json{
|
||||
{
|
||||
"fileId": srcFile.Id,
|
||||
"fileName": dstName,
|
||||
"isFolder": isFolder,
|
||||
},
|
||||
}
|
||||
taskInfosBytes, err := json.Marshal(taskInfos)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
form := map[string]string{
|
||||
"type": "COPY",
|
||||
"targetFolderId": dstDirFile.Id,
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Cloud189) Delete(path string, account *model.Account) error {
|
||||
path = utils.ParsePath(path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
isFolder := 0
|
||||
if file.IsDir() {
|
||||
isFolder = 1
|
||||
}
|
||||
taskInfos := []base.Json{
|
||||
{
|
||||
"fileId": file.Id,
|
||||
"fileName": file.Name,
|
||||
"isFolder": isFolder,
|
||||
},
|
||||
}
|
||||
taskInfosBytes, err := json.Marshal(taskInfos)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
form := map[string]string{
|
||||
"type": "DELETE",
|
||||
"targetFolderId": "",
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Upload Error: decrypt encryptionText failed
|
||||
func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
const DEFAULT uint64 = 10485760
|
||||
var count = int64(math.Ceil(float64(file.GetSize()) / float64(DEFAULT)))
|
||||
var finish uint64 = 0
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
res, err := driver.UploadRequest("/person/initMultiUpload", map[string]string{
|
||||
"parentFolderId": parentFile.Id,
|
||||
"fileName": file.Name,
|
||||
"fileSize": strconv.FormatInt(int64(file.Size), 10),
|
||||
"sliceSize": strconv.FormatInt(int64(DEFAULT), 10),
|
||||
"lazyCheck": "1",
|
||||
}, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uploadFileId := jsoniter.Get(res, "data.uploadFileId").ToString()
|
||||
var i int64
|
||||
var byteSize uint64
|
||||
md5s := make([]string, 0)
|
||||
md5Sum := md5.New()
|
||||
for i = 1; i <= count; i++ {
|
||||
byteSize = file.GetSize() - finish
|
||||
if DEFAULT < byteSize {
|
||||
byteSize = DEFAULT
|
||||
}
|
||||
log.Debugf("%d,%d", byteSize, finish)
|
||||
byteData := make([]byte, byteSize)
|
||||
n, err := io.ReadFull(file, byteData)
|
||||
log.Debug(err, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
finish += uint64(n)
|
||||
md5Bytes := getMd5(byteData)
|
||||
md5Str := hex.EncodeToString(md5Bytes)
|
||||
md5Base64 := base64.StdEncoding.EncodeToString(md5Bytes)
|
||||
md5s = append(md5s, md5Str)
|
||||
md5Sum.Write(byteData)
|
||||
res, err = driver.UploadRequest("/person/getMultiUploadUrls", map[string]string{
|
||||
"partInfo": fmt.Sprintf("%s-%s", strconv.FormatInt(i, 10), md5Base64),
|
||||
"uploadFileId": uploadFileId,
|
||||
}, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uploadData := jsoniter.Get(res, "uploadUrls.partNumber_"+strconv.FormatInt(i, 10))
|
||||
headers := strings.Split(uploadData.Get("requestHeader").ToString(), "&")
|
||||
req, err := http.NewRequest("PUT", uploadData.Get("requestURL").ToString(), bytes.NewBuffer(byteData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, header := range headers {
|
||||
kv := strings.Split(header, "=")
|
||||
req.Header.Set(kv[0], strings.Join(kv[1:], "="))
|
||||
}
|
||||
res, err := base.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("%+v", res)
|
||||
}
|
||||
id := md5Sum.Sum(nil)
|
||||
res, err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
|
||||
"uploadFileId": uploadFileId,
|
||||
"fileMd5": hex.EncodeToString(id),
|
||||
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s, "\n")),
|
||||
"lazyCheck": "1",
|
||||
}, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Cloud189)(nil)
|
@ -1,200 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Cloud189 struct {}
|
||||
|
||||
func (driver Cloud189) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "189Cloud",
|
||||
OnlyProxy: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Cloud189) Items() []Item {
|
||||
return []Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "account username/phone number",
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "account password",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Values: "name,size,lastOpTime,createdDate",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "desc",
|
||||
Type: "select",
|
||||
Values: "true,false",
|
||||
Required: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Cloud189) Save(account *model.Account, old *model.Account) error {
|
||||
if old != nil && old.Name != account.Name {
|
||||
delete(client189Map, old.Name)
|
||||
}
|
||||
if err := driver.Login(account); err != nil {
|
||||
account.Status = err.Error()
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
account.Status = "work"
|
||||
err := model.SaveAccount(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
}
|
||||
|
||||
func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []Cloud189File
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]Cloud189File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.File(utils.ParsePath(path), account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if file.Type == conf.FOLDER {
|
||||
return "", NotFile
|
||||
}
|
||||
client, ok := client189Map[account.Name]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("can't find [%s] client", account.Name)
|
||||
}
|
||||
var e Cloud189Error
|
||||
var resp Cloud189Down
|
||||
_, err = client.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("Accept", "application/json;charset=UTF-8").
|
||||
SetQueryParams(map[string]string{
|
||||
"noCache": random(),
|
||||
"fileId": file.Id,
|
||||
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if e.ErrorCode != "" {
|
||||
if e.ErrorCode == "InvalidSessionKey" {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
}
|
||||
if resp.ResCode != 0 {
|
||||
return "", fmt.Errorf(resp.ResMessage)
|
||||
}
|
||||
res, err := NoRedirectClient.R().Get(resp.FileDownloadUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if res.StatusCode() == 302 {
|
||||
return res.Header().Get("location"), nil
|
||||
}
|
||||
return resp.FileDownloadUrl, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("189 path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Proxy(ctx *gin.Context, account *model.Account) {
|
||||
ctx.Request.Header.Del("Origin")
|
||||
}
|
||||
|
||||
func (driver Cloud189) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*Cloud189)(nil)
|
@ -1,250 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/robfig/cron/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type AliDrive struct{}
|
||||
|
||||
func (driver AliDrive) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "AliDrive",
|
||||
OnlyProxy: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver AliDrive) Items() []Item {
|
||||
return []Item{
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Values: "name,size,updated_at,created_at",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: "select",
|
||||
Values: "ASC,DESC",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "refresh_token",
|
||||
Label: "refresh token",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: "string",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "limit",
|
||||
Label: "limit",
|
||||
Type: "number",
|
||||
Required: false,
|
||||
Description: ">0 and <=200",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
|
||||
if old != nil {
|
||||
conf.Cron.Remove(cron.EntryID(old.CronId))
|
||||
}
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "root"
|
||||
}
|
||||
if account.Limit == 0 {
|
||||
account.Limit = 200
|
||||
}
|
||||
err := driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp Json
|
||||
_, _ = aliClient.R().SetResult(&resp).
|
||||
SetBody("{}").
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
Post("https://api.aliyundrive.com/v2/user/get")
|
||||
log.Debugf("user info: %+v", resp)
|
||||
account.DriveId = resp["default_drive_id"].(string)
|
||||
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
|
||||
name := account.Name
|
||||
log.Debugf("ali account name: %s", name)
|
||||
newAccount, ok := model.GetAccount(name)
|
||||
log.Debugf("ali account: %+v", newAccount)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
err = driver.RefreshToken(&newAccount)
|
||||
_ = model.SaveAccount(&newAccount)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
account.CronId = int(cronId)
|
||||
err = model.SaveAccount(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
}
|
||||
|
||||
func (driver AliDrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []AliFile
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]AliFile)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var resp Json
|
||||
var e AliRespError
|
||||
_, err = aliClient.R().SetResult(&resp).
|
||||
SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": file.Id,
|
||||
"expire_sec": 14400,
|
||||
}).Post("https://api.aliyundrive.com/v2/file/get_download_url")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
return resp["url"].(string), nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("ali path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Del("Origin")
|
||||
c.Request.Header.Set("Referer", "https://www.aliyundrive.com/")
|
||||
}
|
||||
|
||||
func (driver AliDrive) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
file, err := driver.GetFile(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// office
|
||||
var resp Json
|
||||
var e AliRespError
|
||||
var url string
|
||||
req := Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": file.FileId,
|
||||
}
|
||||
switch file.Category {
|
||||
case "doc":
|
||||
{
|
||||
url = "https://api.aliyundrive.com/v2/file/get_office_preview_url"
|
||||
req["access_token"] = account.AccessToken
|
||||
}
|
||||
case "video":
|
||||
{
|
||||
url = "https://api.aliyundrive.com/v2/file/get_video_preview_play_info"
|
||||
req["category"] = "live_transcoding"
|
||||
}
|
||||
default:
|
||||
return nil, NotSupport
|
||||
}
|
||||
_, err = aliClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(req).Post(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Code != "" {
|
||||
return nil, fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
var _ Driver = (*AliDrive)(nil)
|
@ -1,13 +1,16 @@
|
||||
package drivers
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -75,7 +78,7 @@ func (driver AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFil
|
||||
SetResult(&resp).
|
||||
SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(Json{
|
||||
SetBody(base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"fields": "*",
|
||||
"image_thumbnail_process": "image/resize,w_400/format,jpeg",
|
||||
@ -116,7 +119,7 @@ func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, e
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
||||
parentFiles_, _ := base.GetCache(dir, account)
|
||||
parentFiles, _ := parentFiles_.([]AliFile)
|
||||
for _, file := range parentFiles {
|
||||
if file.Name == name {
|
||||
@ -127,16 +130,16 @@ func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, e
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver AliDrive) RefreshToken(account *model.Account) error {
|
||||
url := "https://auth.aliyundrive.com/v2/account/token"
|
||||
var resp TokenResp
|
||||
var resp base.TokenResp
|
||||
var e AliRespError
|
||||
_, err := aliClient.R().
|
||||
//ForceContentType("application/json").
|
||||
SetBody(Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
|
||||
SetBody(base.Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
|
||||
SetResult(&resp).
|
||||
SetError(&e).
|
||||
Post(url)
|
||||
@ -155,11 +158,86 @@ func (driver AliDrive) RefreshToken(account *model.Account) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Rename(fileId, name string, account *model.Account) error {
|
||||
var resp base.Json
|
||||
var e AliRespError
|
||||
_, err := aliClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"check_name_mode": "refuse",
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": fileId,
|
||||
"name": name,
|
||||
}).Post("https://api.aliyundrive.com/v3/file/update")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Rename(fileId, name, account)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
if resp["name"] == name {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%+v", resp)
|
||||
}
|
||||
|
||||
func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
|
||||
var e AliRespError
|
||||
res, err := aliClient.R().SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"requests": []base.Json{
|
||||
{
|
||||
"headers": base.Json{
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
"method":"POST",
|
||||
"id":srcId,
|
||||
"body":base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id":srcId,
|
||||
"to_drive_id":account.DriveId,
|
||||
"to_parent_file_id":dstId,
|
||||
},
|
||||
},
|
||||
},
|
||||
"resource": "file",
|
||||
}).Post("https://api.aliyundrive.com/v3/batch")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Batch(srcId, dstId, account)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
if strings.Contains(res.String(), `"status":200`) {
|
||||
return nil
|
||||
}
|
||||
return errors.New(res.String())
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&AliDrive{})
|
||||
base.RegisterDriver(&AliDrive{})
|
||||
aliClient.
|
||||
SetRetryCount(3).
|
||||
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").
|
||||
SetHeader("content-type", "application/json").
|
||||
SetHeader("origin", "https://aliyundrive.com")
|
||||
SetHeader("origin", "https://www.aliyundrive.com")
|
||||
}
|
480
drivers/alidrive/driver.go
Normal file
480
drivers/alidrive/driver.go
Normal file
@ -0,0 +1,480 @@
|
||||
package alidrive
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/robfig/cron/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type AliDrive struct{}
|
||||
|
||||
func (driver AliDrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "AliDrive",
|
||||
}
|
||||
}
|
||||
|
||||
func (driver AliDrive) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "refresh_token",
|
||||
Label: "refresh token",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: base.TypeSelect,
|
||||
Values: "name,size,updated_at,created_at",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: base.TypeSelect,
|
||||
Values: "ASC,DESC",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "limit",
|
||||
Label: "limit",
|
||||
Type: base.TypeNumber,
|
||||
Required: false,
|
||||
Description: ">0 and <=200",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
|
||||
if old != nil {
|
||||
conf.Cron.Remove(cron.EntryID(old.CronId))
|
||||
}
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "root"
|
||||
}
|
||||
if account.Limit == 0 {
|
||||
account.Limit = 200
|
||||
}
|
||||
err := driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp base.Json
|
||||
_, _ = aliClient.R().SetResult(&resp).
|
||||
SetBody("{}").
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
Post("https://api.aliyundrive.com/v2/user/get")
|
||||
log.Debugf("user info: %+v", resp)
|
||||
account.DriveId = resp["default_drive_id"].(string)
|
||||
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
|
||||
name := account.Name
|
||||
log.Debugf("ali account name: %s", name)
|
||||
newAccount, ok := model.GetAccount(name)
|
||||
log.Debugf("ali account: %+v", newAccount)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
err = driver.RefreshToken(&newAccount)
|
||||
_ = model.SaveAccount(&newAccount)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
account.CronId = int(cronId)
|
||||
err = model.SaveAccount(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver AliDrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []AliFile
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]AliFile)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = base.SetCache(path, rawFiles, account)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp base.Json
|
||||
var e AliRespError
|
||||
_, err = aliClient.R().SetResult(&resp).
|
||||
SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": file.Id,
|
||||
"expire_sec": 14400,
|
||||
}).Post("https://api.aliyundrive.com/v2/file/get_download_url")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Link(args, account)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
return &base.Link{
|
||||
Url: resp["url"].(string),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("ali path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Del("Origin")
|
||||
c.Request.Header.Set("Referer", "https://www.aliyundrive.com/")
|
||||
}
|
||||
|
||||
func (driver AliDrive) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
file, err := driver.GetFile(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// office
|
||||
var resp base.Json
|
||||
var e AliRespError
|
||||
var url string
|
||||
req := base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": file.FileId,
|
||||
}
|
||||
switch file.Category {
|
||||
case "doc":
|
||||
{
|
||||
url = "https://api.aliyundrive.com/v2/file/get_office_preview_url"
|
||||
req["access_token"] = account.AccessToken
|
||||
}
|
||||
case "video":
|
||||
{
|
||||
url = "https://api.aliyundrive.com/v2/file/get_video_preview_play_info"
|
||||
req["category"] = "live_transcoding"
|
||||
}
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
_, err = aliClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(req).Post(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Code != "" {
|
||||
return nil, fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) MakeDir(path string, account *model.Account) error {
|
||||
dir, name := filepath.Split(path)
|
||||
parentFile, err := driver.File(dir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
var resp base.Json
|
||||
var e AliRespError
|
||||
_, err = aliClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"check_name_mode": "refuse",
|
||||
"drive_id": account.DriveId,
|
||||
"name": name,
|
||||
"parent_file_id": parentFile.Id,
|
||||
"type": "folder",
|
||||
}).Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders")
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.MakeDir(path, account)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
if resp["file_name"] == name {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%+v", resp)
|
||||
}
|
||||
|
||||
func (driver AliDrive) Move(src string, dst string, account *model.Account) error {
|
||||
srcDir, _ := filepath.Split(src)
|
||||
dstDir, dstName := filepath.Split(dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// rename
|
||||
if srcDir == dstDir {
|
||||
err = driver.Rename(srcFile.Id, dstName, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = driver.Batch(srcFile.Id, dstDirFile.Id, account)
|
||||
}
|
||||
if err != nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver AliDrive) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver AliDrive) Delete(path string, account *model.Account) error {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var e AliRespError
|
||||
res, err := aliClient.R().SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": file.Id,
|
||||
}).Post("https://api.aliyundrive.com/v2/recyclebin/trash")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Delete(path, account)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
if res.StatusCode() == 204 {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
return nil
|
||||
}
|
||||
return errors.New(res.String())
|
||||
}
|
||||
|
||||
type UploadResp struct {
|
||||
FileId string `json:"file_id"`
|
||||
UploadId string `json:"upload_id"`
|
||||
PartInfoList []struct {
|
||||
UploadUrl string `json:"upload_url"`
|
||||
} `json:"part_info_list"`
|
||||
}
|
||||
|
||||
func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
const DEFAULT uint64 = 10485760
|
||||
var count = int64(math.Ceil(float64(file.GetSize()) / float64(DEFAULT)))
|
||||
var finish uint64 = 0
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
var resp UploadResp
|
||||
var e AliRespError
|
||||
partInfoList := make([]base.Json, 0)
|
||||
var i int64
|
||||
for i = 0; i < count; i++ {
|
||||
partInfoList = append(partInfoList, base.Json{
|
||||
"part_number": i + 1,
|
||||
})
|
||||
}
|
||||
_, err = aliClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"check_name_mode": "auto_rename",
|
||||
// content_hash
|
||||
"content_hash_name": "none",
|
||||
"drive_id": account.DriveId,
|
||||
"name": file.GetFileName(),
|
||||
"parent_file_id": parentFile.Id,
|
||||
"part_info_list": partInfoList,
|
||||
//proof_code
|
||||
"proof_version": "v1",
|
||||
"size": file.GetSize(),
|
||||
"type": "file",
|
||||
}).Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders") // /v2/file/create_with_proof
|
||||
//log.Debugf("%+v\n%+v", resp, e)
|
||||
if e.Code != "" {
|
||||
if e.Code == "AccessTokenInvalid" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Upload(file, account)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
var byteSize uint64
|
||||
for i = 0; i < count; i++ {
|
||||
byteSize = file.GetSize() - finish
|
||||
if DEFAULT < byteSize {
|
||||
byteSize = DEFAULT
|
||||
}
|
||||
log.Debugf("%d,%d", byteSize, finish)
|
||||
byteData := make([]byte, byteSize)
|
||||
n, err := io.ReadFull(file, byteData)
|
||||
//n, err := file.Read(byteData)
|
||||
//byteData, err := io.ReadAll(file)
|
||||
//n := len(byteData)
|
||||
log.Debug(err, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
finish += uint64(n)
|
||||
|
||||
req, err := http.NewRequest("PUT", resp.PartInfoList[i].UploadUrl, bytes.NewBuffer(byteData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := base.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("%+v", res)
|
||||
//res, err := base.BaseClient.R().
|
||||
// SetHeader("Content-Type","").
|
||||
// SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
//log.Debugf("put to %s : %d,%s", resp.PartInfoList[i].UploadUrl, res.StatusCode(),res.String())
|
||||
}
|
||||
var resp2 base.Json
|
||||
_, err = aliClient.R().SetResult(&resp2).SetError(&e).
|
||||
SetHeader("authorization", "Bearer\t"+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"drive_id": account.DriveId,
|
||||
"file_id": resp.FileId,
|
||||
"upload_id": resp.UploadId,
|
||||
}).Post("https://api.aliyundrive.com/v2/file/complete")
|
||||
if e.Code != "" {
|
||||
//if e.Code == "AccessTokenInvalid" {
|
||||
// err = driver.RefreshToken(account)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// } else {
|
||||
// _ = model.SaveAccount(account)
|
||||
// return driver.Upload(file, account)
|
||||
// }
|
||||
//}
|
||||
return fmt.Errorf("%s", e.Message)
|
||||
}
|
||||
if resp2["file_id"] == resp.FileId {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%+v", resp2)
|
||||
}
|
||||
|
||||
var _ base.Driver = (*AliDrive)(nil)
|
40
drivers/alist/alist.go
Normal file
40
drivers/alist/alist.go
Normal file
@ -0,0 +1,40 @@
|
||||
package alist
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
)
|
||||
|
||||
type BaseResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type PathResp struct {
|
||||
BaseResp
|
||||
Data []model.File `json:"data"`
|
||||
}
|
||||
|
||||
type PreviewResp struct {
|
||||
BaseResp
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func (driver *Alist) Login(account *model.Account) error {
|
||||
var resp BaseResp
|
||||
_, err := base.RestyClient.R().SetResult(&resp).
|
||||
SetHeader("Authorization", account.AccessToken).
|
||||
Get(account.SiteUrl+"/api/admin/login")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Code != 200 {
|
||||
return errors.New(resp.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Alist{})
|
||||
}
|
186
drivers/alist/driver.go
Normal file
186
drivers/alist/driver.go
Normal file
@ -0,0 +1,186 @@
|
||||
package alist
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Alist struct{}
|
||||
|
||||
func (driver Alist) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Alist",
|
||||
NoNeedSetLink: true,
|
||||
NoCors: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Alist) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "site_url",
|
||||
Label: "alist site url",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "access_token",
|
||||
Label: "token",
|
||||
Type: base.TypeString,
|
||||
Description: "admin token",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder path",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Alist) Save(account *model.Account, old *model.Account) error {
|
||||
account.SiteUrl = strings.TrimRight(account.SiteUrl, "/")
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "/"
|
||||
}
|
||||
err := driver.Login(account)
|
||||
if err == nil {
|
||||
account.Status = "work"
|
||||
} else {
|
||||
account.Status = err.Error()
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Alist) File(path string, account *model.Account) (*model.File, error) {
|
||||
now := time.Now()
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: "root",
|
||||
Name: "root",
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: &now,
|
||||
}, nil
|
||||
}
|
||||
_, files, err := driver.Path(utils.Dir(path), account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if files == nil {
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
name := utils.Base(path)
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Alist) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
//return nil, base.ErrNotImplement
|
||||
_, files, err := driver.Path(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if files == nil {
|
||||
return nil, base.ErrNotFolder
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Alist) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
path = utils.ParsePath(path)
|
||||
name := utils.Base(path)
|
||||
flag := "d"
|
||||
if utils.GetFileType(filepath.Ext(path)) == conf.TEXT {
|
||||
flag = "p"
|
||||
}
|
||||
link := base.Link{}
|
||||
link.Url = fmt.Sprintf("%s/%s%s?sign=%s", account.SiteUrl, flag, path, utils.SignWithToken(name, conf.Token))
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Alist) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
path = filepath.Join(account.RootFolder, path)
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files := cache.([]model.File)
|
||||
return nil, files, nil
|
||||
}
|
||||
var resp PathResp
|
||||
_, err = base.RestyClient.R().SetResult(&resp).
|
||||
SetHeader("Authorization", account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"path": path,
|
||||
}).Post(account.SiteUrl + "/api/public/path")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if resp.Code != 200 {
|
||||
return nil, nil, errors.New(resp.Message)
|
||||
}
|
||||
if resp.Message == "file" {
|
||||
return &resp.Data[0], nil, nil
|
||||
}
|
||||
if len(resp.Data) > 0 {
|
||||
_ = base.SetCache(path, resp.Data, account)
|
||||
}
|
||||
return nil, resp.Data, nil
|
||||
}
|
||||
|
||||
func (driver Alist) Proxy(c *gin.Context, account *model.Account) {}
|
||||
|
||||
func (driver Alist) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
var resp PathResp
|
||||
_, err := base.RestyClient.R().SetResult(&resp).
|
||||
SetHeader("Authorization", account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"path": path,
|
||||
}).Post(account.SiteUrl + "/api/public/preview")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Code != 200 {
|
||||
return nil, errors.New(resp.Message)
|
||||
}
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (driver Alist) MakeDir(path string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver Alist) Move(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver Alist) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver Alist) Delete(path string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver Alist) Upload(file *model.FileStream, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Alist)(nil)
|
41
drivers/all.go
Normal file
41
drivers/all.go
Normal file
@ -0,0 +1,41 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
_ "github.com/Xhofe/alist/drivers/123"
|
||||
_ "github.com/Xhofe/alist/drivers/189"
|
||||
_ "github.com/Xhofe/alist/drivers/alidrive"
|
||||
_ "github.com/Xhofe/alist/drivers/alist"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
_ "github.com/Xhofe/alist/drivers/ftp"
|
||||
_ "github.com/Xhofe/alist/drivers/google"
|
||||
_ "github.com/Xhofe/alist/drivers/lanzou"
|
||||
_ "github.com/Xhofe/alist/drivers/native"
|
||||
_ "github.com/Xhofe/alist/drivers/onedrive"
|
||||
_ "github.com/Xhofe/alist/drivers/pikpak"
|
||||
_ "github.com/Xhofe/alist/drivers/s3"
|
||||
_ "github.com/Xhofe/alist/drivers/shandian"
|
||||
_ "github.com/Xhofe/alist/drivers/webdav"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var NoCors string
|
||||
var NoUpload string
|
||||
|
||||
func GetConfig() {
|
||||
for k, v := range base.GetDriversMap() {
|
||||
if v.Config().NoCors {
|
||||
NoCors += k + ","
|
||||
}
|
||||
if v.Upload(nil, nil) != base.ErrEmptyFile {
|
||||
NoUpload += k + ","
|
||||
}
|
||||
}
|
||||
NoCors = strings.Trim(NoCors, ",")
|
||||
NoUpload = strings.Trim(NoUpload, ",")
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.Debug("all init")
|
||||
GetConfig()
|
||||
}
|
28
drivers/base/cache.go
Normal file
28
drivers/base/cache.go
Normal file
@ -0,0 +1,28 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func KeyCache(path string, account *model.Account) string {
|
||||
path = utils.ParsePath(path)
|
||||
return fmt.Sprintf("%s%s", account.Name, path)
|
||||
}
|
||||
|
||||
func SetCache(path string, obj interface{}, account *model.Account) error {
|
||||
return conf.Cache.Set(conf.Ctx, KeyCache(path, account), obj, nil)
|
||||
}
|
||||
|
||||
func GetCache(path string, account *model.Account) (interface{}, error) {
|
||||
return conf.Cache.Get(conf.Ctx, KeyCache(path, account))
|
||||
}
|
||||
|
||||
func DeleteCache(path string, account *model.Account) error {
|
||||
err := conf.Cache.Delete(conf.Ctx, KeyCache(path, account))
|
||||
log.Debugf("delete cache %s: %+v", path, err)
|
||||
return err
|
||||
}
|
166
drivers/base/driver.go
Normal file
166
drivers/base/driver.go
Normal file
@ -0,0 +1,166 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DriverConfig struct {
|
||||
Name string
|
||||
OnlyProxy bool // 必须使用代理(本机或者其他机器)
|
||||
OnlyLocal bool // 必须本机返回的
|
||||
ApiProxy bool // 使用API中转的
|
||||
NoNeedSetLink bool // 不需要设置链接的
|
||||
NoCors bool // 不可以跨域
|
||||
LocalSort bool // 本地排序
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
Path string
|
||||
IP string
|
||||
}
|
||||
|
||||
type Driver interface {
|
||||
// Config 配置
|
||||
Config() DriverConfig
|
||||
// Items 账号所需参数
|
||||
Items() []Item
|
||||
// Save 保存时处理
|
||||
Save(account *model.Account, old *model.Account) error
|
||||
// File 取文件
|
||||
File(path string, account *model.Account) (*model.File, error)
|
||||
// Files 取文件夹
|
||||
Files(path string, account *model.Account) ([]model.File, error)
|
||||
// Link 取链接
|
||||
Link(args Args, account *model.Account) (*Link, error)
|
||||
// Path 取路径(文件或文件夹)
|
||||
Path(path string, account *model.Account) (*model.File, []model.File, error)
|
||||
// Proxy 代理处理
|
||||
Proxy(c *gin.Context, account *model.Account)
|
||||
// Preview 预览
|
||||
Preview(path string, account *model.Account) (interface{}, error)
|
||||
// MakeDir 创建文件夹
|
||||
MakeDir(path string, account *model.Account) error
|
||||
// Move 移动/改名
|
||||
Move(src string, dst string, account *model.Account) error
|
||||
// Copy 拷贝
|
||||
Copy(src string, dst string, account *model.Account) error
|
||||
// Delete 删除
|
||||
Delete(path string, account *model.Account) error
|
||||
// Upload 上传
|
||||
Upload(file *model.FileStream, account *model.Account) error
|
||||
// TODO
|
||||
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Name string `json:"name"`
|
||||
Label string `json:"label"`
|
||||
Type string `json:"type"`
|
||||
Values string `json:"values"`
|
||||
Required bool `json:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
var driversMap = map[string]Driver{}
|
||||
|
||||
func RegisterDriver(driver Driver) {
|
||||
log.Infof("register driver: [%s]", driver.Config().Name)
|
||||
driversMap[driver.Config().Name] = driver
|
||||
}
|
||||
|
||||
func GetDriver(name string) (driver Driver, ok bool) {
|
||||
driver, ok = driversMap[name]
|
||||
return
|
||||
}
|
||||
|
||||
func GetDriversMap() map[string]Driver {
|
||||
return driversMap
|
||||
}
|
||||
|
||||
func GetDrivers() map[string][]Item {
|
||||
res := make(map[string][]Item, 0)
|
||||
for k, v := range driversMap {
|
||||
if v.Config().OnlyProxy {
|
||||
res[k] = v.Items()
|
||||
} else {
|
||||
res[k] = append([]Item{
|
||||
//{
|
||||
// Name: "allow_proxy",
|
||||
// Label: "allow_proxy",
|
||||
// Type: TypeBool,
|
||||
// Required: true,
|
||||
// Description: "allow proxy",
|
||||
//},
|
||||
{
|
||||
Name: "proxy",
|
||||
Label: "proxy",
|
||||
Type: TypeBool,
|
||||
Required: true,
|
||||
Description: "web proxy",
|
||||
},
|
||||
{
|
||||
Name: "webdav_proxy",
|
||||
Label: "webdav proxy",
|
||||
Type: TypeBool,
|
||||
Required: true,
|
||||
Description: "Transfer the WebDAV of this account through the server",
|
||||
},
|
||||
}, v.Items()...)
|
||||
}
|
||||
res[k] = append([]Item{
|
||||
{
|
||||
Name: "down_proxy_url",
|
||||
Label: "down_proxy_url",
|
||||
Type: TypeString,
|
||||
},
|
||||
}, res[k]...)
|
||||
if v.Config().ApiProxy {
|
||||
res[k] = append([]Item{
|
||||
{
|
||||
Name: "api_proxy_url",
|
||||
Label: "api_proxy_url",
|
||||
Type: TypeString,
|
||||
},
|
||||
}, res[k]...)
|
||||
}
|
||||
if v.Config().LocalSort {
|
||||
res[k] = append(res[k], []Item{
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: TypeSelect,
|
||||
Values: "name,size,updated_at",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: TypeSelect,
|
||||
Values: "ASC,DESC",
|
||||
Required: false,
|
||||
},
|
||||
}...)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
var NoRedirectClient *resty.Client
|
||||
var RestyClient = resty.New()
|
||||
var HttpClient = &http.Client{}
|
||||
|
||||
func init() {
|
||||
NoRedirectClient = resty.New().SetRedirectPolicy(
|
||||
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}),
|
||||
)
|
||||
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
||||
NoRedirectClient.SetHeader("user-agent", userAgent)
|
||||
RestyClient.SetHeader("user-agent", userAgent)
|
||||
RestyClient.SetRetryCount(3)
|
||||
}
|
48
drivers/base/types.go
Normal file
48
drivers/base/types.go
Normal file
@ -0,0 +1,48 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPathNotFound = errors.New("path not found")
|
||||
ErrNotFile = errors.New("not file")
|
||||
ErrNotImplement = errors.New("not implement")
|
||||
ErrNotSupport = errors.New("not support")
|
||||
ErrNotFolder = errors.New("not a folder")
|
||||
ErrEmptyFile = errors.New("empty file")
|
||||
)
|
||||
|
||||
const (
|
||||
TypeString = "string"
|
||||
TypeSelect = "select"
|
||||
TypeBool = "bool"
|
||||
TypeNumber = "number"
|
||||
)
|
||||
|
||||
const (
|
||||
Get = iota
|
||||
Post
|
||||
Put
|
||||
Delete
|
||||
Patch
|
||||
)
|
||||
|
||||
type Json map[string]interface{}
|
||||
|
||||
type TokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
type Header struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
Url string `json:"url"`
|
||||
Headers []Header `json:"headers"`
|
||||
Data io.ReadCloser
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DriverConfig struct {
|
||||
Name string
|
||||
OnlyProxy bool
|
||||
}
|
||||
|
||||
type Driver interface {
|
||||
Config() DriverConfig
|
||||
Items() []Item
|
||||
Save(account *model.Account, old *model.Account) error
|
||||
File(path string, account *model.Account) (*model.File, error)
|
||||
Files(path string, account *model.Account) ([]model.File, error)
|
||||
Link(path string, account *model.Account) (string, error)
|
||||
Path(path string, account *model.Account) (*model.File, []model.File, error)
|
||||
Proxy(c *gin.Context, account *model.Account)
|
||||
Preview(path string, account *model.Account) (interface{}, error)
|
||||
// TODO
|
||||
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
|
||||
//MakeDir(path string, account *model.Account) error
|
||||
//Move(src string, des string, account *model.Account) error
|
||||
//Delete(path string) error
|
||||
//Upload(file *fs.File, path string, account *model.Account) error
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Name string `json:"name"`
|
||||
Label string `json:"label"`
|
||||
Type string `json:"type"`
|
||||
Values string `json:"values"`
|
||||
Required bool `json:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type TokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
var driversMap = map[string]Driver{}
|
||||
|
||||
func RegisterDriver(driver Driver) {
|
||||
log.Infof("register driver: [%s]", driver.Config().Name)
|
||||
driversMap[driver.Config().Name] = driver
|
||||
}
|
||||
|
||||
func GetDriver(name string) (driver Driver, ok bool) {
|
||||
driver, ok = driversMap[name]
|
||||
return
|
||||
}
|
||||
|
||||
func GetDrivers() map[string][]Item {
|
||||
res := make(map[string][]Item, 0)
|
||||
for k, v := range driversMap {
|
||||
if v.Config().OnlyProxy {
|
||||
res[k] = v.Items()
|
||||
} else {
|
||||
res[k] = append([]Item{
|
||||
{
|
||||
Name: "proxy",
|
||||
Label: "proxy",
|
||||
Type: "bool",
|
||||
Required: true,
|
||||
Description: "allow proxy",
|
||||
},
|
||||
{
|
||||
Name: "webdav_proxy",
|
||||
Label: "webdav proxy",
|
||||
Type: "bool",
|
||||
Required: true,
|
||||
Description: "Transfer the WebDAV of this account through the server",
|
||||
},
|
||||
}, v.Items()...)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
type Json map[string]interface{}
|
||||
|
||||
var NoRedirectClient *resty.Client
|
||||
|
||||
func init() {
|
||||
NoRedirectClient = resty.New().SetRedirectPolicy(
|
||||
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}),
|
||||
)
|
||||
NoRedirectClient.SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||
}
|
259
drivers/ftp/driver.go
Normal file
259
drivers/ftp/driver.go
Normal file
@ -0,0 +1,259 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jlaffaye/ftp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FTP struct{}
|
||||
|
||||
func (driver FTP) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "FTP",
|
||||
OnlyProxy: true,
|
||||
OnlyLocal: true,
|
||||
NoNeedSetLink: true,
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver FTP) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "site_url",
|
||||
Label: "ftp host url",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder path",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver FTP) Save(account *model.Account, old *model.Account) error {
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "/"
|
||||
}
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
} else {
|
||||
account.Status = "work"
|
||||
_ = conn.Quit()
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver FTP) File(path string, account *model.Account) (*model.File, error) {
|
||||
log.Debugf("file: %s", path)
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver FTP) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
log.Debugf("files: %s", path)
|
||||
path = utils.ParsePath(path)
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ := cache.([]model.File)
|
||||
return files, nil
|
||||
}
|
||||
realPath := utils.Join(account.RootFolder, path)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
entries, err := conn.List(realPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := make([]model.File, 0)
|
||||
for i, _ := range entries {
|
||||
entry := entries[i]
|
||||
if entry.Name == "." || entry.Name == ".." {
|
||||
continue
|
||||
}
|
||||
f := model.File{
|
||||
Name: entry.Name,
|
||||
Size: int64(entry.Size),
|
||||
UpdatedAt: &entry.Time,
|
||||
Driver: driver.Config().Name,
|
||||
}
|
||||
if entry.Type == ftp.EntryTypeFolder {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
f.Type = utils.GetFileType(filepath.Ext(entry.Name))
|
||||
}
|
||||
res = append(res, f)
|
||||
}
|
||||
if len(res) > 0 {
|
||||
_ = base.SetCache(path, res, account)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver FTP) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
path = utils.ParsePath(path)
|
||||
realPath := utils.Join(account.RootFolder, path)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
resp, err := conn.Retr(realPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//defer func() { _ = resp.Close() }()
|
||||
//data, err := ioutil.ReadAll(resp)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
return &base.Link{
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (driver FTP) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
log.Debugf("ftp path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver FTP) Proxy(c *gin.Context, account *model.Account) {
|
||||
|
||||
}
|
||||
|
||||
func (driver FTP) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver FTP) MakeDir(path string, account *model.Account) error {
|
||||
path = utils.ParsePath(path)
|
||||
realPath := utils.Join(account.RootFolder, path)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
err = conn.MakeDir(realPath)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver FTP) Move(src string, dst string, account *model.Account) error {
|
||||
//if utils.Dir(src) != utils.Dir(dst) {
|
||||
// return base.ErrNotSupport
|
||||
//}
|
||||
realSrc := utils.Join(account.RootFolder, src)
|
||||
realDst := utils.Join(account.RootFolder, dst)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
err = conn.Rename(realSrc, realDst)
|
||||
if err != nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver FTP) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver FTP) Delete(path string, account *model.Account) error {
|
||||
path = utils.ParsePath(path)
|
||||
realPath := utils.Join(account.RootFolder, path)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
err = conn.Delete(realPath)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver FTP) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
realPath := utils.Join(account.RootFolder, file.ParentPath, file.Name)
|
||||
conn, err := driver.Login(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = conn.Quit() }()
|
||||
err = conn.Stor(realPath, file)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(file.ParentPath), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*FTP)(nil)
|
23
drivers/ftp/ftp.go
Normal file
23
drivers/ftp/ftp.go
Normal file
@ -0,0 +1,23 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/jlaffaye/ftp"
|
||||
)
|
||||
|
||||
func (driver FTP) Login(account *model.Account) (*ftp.ServerConn, error) {
|
||||
conn, err := ftp.Connect(account.SiteUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = conn.Login(account.Username, account.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&FTP{})
|
||||
}
|
295
drivers/google/driver.go
Normal file
295
drivers/google/driver.go
Normal file
@ -0,0 +1,295 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type GoogleDrive struct{}
|
||||
|
||||
func (driver GoogleDrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "GoogleDrive",
|
||||
OnlyProxy: true,
|
||||
ApiProxy: true,
|
||||
NoNeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "client_id",
|
||||
Label: "client id",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "client_secret",
|
||||
Label: "client secret",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "refresh_token",
|
||||
Label: "refresh token",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
Description: "such as: folder,name,modifiedTime",
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: base.TypeSelect,
|
||||
Values: "asc,desc",
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Save(account *model.Account, old *model.Account) error {
|
||||
account.Proxy = true
|
||||
err := driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "root"
|
||||
}
|
||||
account.Status = "work"
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = base.SetCache(path, rawFiles, account)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file, account))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file.Type == conf.FOLDER {
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
url := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id)
|
||||
_, err = driver.Request(url, base.Get, nil, nil, nil, nil, nil, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link := base.Link{
|
||||
Url: url + "&alt=media",
|
||||
Headers: []base.Header{
|
||||
{
|
||||
Name: "Authorization",
|
||||
Value: "Bearer " + account.AccessToken,
|
||||
},
|
||||
},
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("google path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Add("Authorization", "Bearer "+account.AccessToken)
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) MakeDir(path string, account *model.Account) error {
|
||||
parentFile, err := driver.File(utils.Dir(path), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"name": utils.Base(path),
|
||||
"parents": []string{parentFile.Id},
|
||||
"mimeType": "application/vnd.google-apps.folder",
|
||||
}
|
||||
_, err = driver.Request("https://www.googleapis.com/drive/v3/files", base.Post, nil, nil, nil, &data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Move(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
url := "https://www.googleapis.com/drive/v3/files/" + srcFile.Id
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if utils.Dir(src) == utils.Dir(dst) {
|
||||
// rename
|
||||
data := base.Json{
|
||||
"name": utils.Base(dst),
|
||||
}
|
||||
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
|
||||
} else {
|
||||
dstParentFile, err := driver.File(utils.Dir(dst), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query := map[string]string{
|
||||
"addParents": dstParentFile.Id,
|
||||
"removeParents": "root",
|
||||
}
|
||||
_, err = driver.Request(url, base.Patch, nil, query, nil, nil, nil, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Delete(path string, account *model.Account) error {
|
||||
file, err := driver.File(path, account)
|
||||
url := "https://www.googleapis.com/drive/v3/files/" + file.Id
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = driver.Request(url, base.Delete, nil, nil, nil, nil, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"name": file.Name,
|
||||
"parents": []string{parentFile.Id},
|
||||
}
|
||||
var e Error
|
||||
url := "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
res, err := base.NoRedirectClient.R().SetHeader("Authorization", "Bearer "+account.AccessToken).
|
||||
SetError(&e).SetBody(data).
|
||||
Post(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
return driver.Upload(file, account)
|
||||
}
|
||||
return fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
putUrl := res.Header().Get("location")
|
||||
byteData, _ := ioutil.ReadAll(file)
|
||||
_, err = driver.Request(putUrl, base.Put, nil, nil, nil, byteData, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*GoogleDrive)(nil)
|
198
drivers/google/googledrive.go
Normal file
198
drivers/google/googledrive.go
Normal file
@ -0,0 +1,198 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TokenError struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) RefreshToken(account *model.Account) error {
|
||||
url := "https://www.googleapis.com/oauth2/v4/token"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var resp base.TokenResp
|
||||
var e TokenError
|
||||
res, err := base.RestyClient.R().SetResult(&resp).SetError(&e).
|
||||
SetFormData(map[string]string{
|
||||
"client_id": account.ClientId,
|
||||
"client_secret": account.ClientSecret,
|
||||
"refresh_token": account.RefreshToken,
|
||||
"grant_type": "refresh_token",
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Error != "" {
|
||||
return fmt.Errorf(e.Error)
|
||||
}
|
||||
account.AccessToken = resp.AccessToken
|
||||
account.Status = "work"
|
||||
return nil
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
MimeType string `json:"mimeType"`
|
||||
ModifiedTime *time.Time `json:"modifiedTime"`
|
||||
Size string `json:"size"`
|
||||
ThumbnailLink string `json:"thumbnailLink"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) IsDir(mimeType string) bool {
|
||||
return mimeType == "application/vnd.google-apps.folder" || mimeType == "application/vnd.google-apps.shortcut"
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) FormatFile(file *File, account *model.Account) *model.File {
|
||||
f := &model.File{
|
||||
Id: file.Id,
|
||||
Name: file.Name,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: file.ModifiedTime,
|
||||
Url: "",
|
||||
}
|
||||
if driver.IsDir(file.MimeType) {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
size, _ := strconv.ParseInt(file.Size, 10, 64)
|
||||
f.Size = size
|
||||
f.Type = utils.GetFileType(filepath.Ext(file.Name))
|
||||
}
|
||||
if file.ThumbnailLink != "" {
|
||||
if account.DownProxyUrl != "" {
|
||||
f.Thumbnail = fmt.Sprintf("%s/%s", account.DownProxyUrl, file.ThumbnailLink)
|
||||
} else {
|
||||
f.Thumbnail = file.ThumbnailLink
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type Files struct {
|
||||
NextPageToken string `json:"nextPageToken"`
|
||||
Files []File `json:"files"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Error struct {
|
||||
Errors []struct {
|
||||
Domain string `json:"domain"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
LocationType string `json:"location_type"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]File, error) {
|
||||
pageToken := "first"
|
||||
res := make([]File, 0)
|
||||
for pageToken != "" {
|
||||
if pageToken == "first" {
|
||||
pageToken = ""
|
||||
}
|
||||
var resp Files
|
||||
orderBy := "folder,name,modifiedTime desc"
|
||||
if account.OrderBy != "" {
|
||||
orderBy = account.OrderBy + " " + account.OrderDirection
|
||||
}
|
||||
query := map[string]string{
|
||||
"orderBy": orderBy,
|
||||
"fields": "files(id,name,mimeType,size,modifiedTime,thumbnailLink),nextPageToken",
|
||||
"pageSize": "1000",
|
||||
"q": fmt.Sprintf("'%s' in parents and trashed = false", id),
|
||||
//"includeItemsFromAllDrives": "true",
|
||||
//"supportsAllDrives": "true",
|
||||
"pageToken": pageToken,
|
||||
}
|
||||
_, err := driver.Request("https://www.googleapis.com/drive/v3/files",
|
||||
base.Get, nil, query, nil, nil, &resp, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pageToken = resp.NextPageToken
|
||||
res = append(res, resp.Files...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Request(url string, method int, headers, query, form map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
req.SetQueryParam("includeItemsFromAllDrives", "true")
|
||||
req.SetQueryParam("supportsAllDrives", "true")
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if form != nil {
|
||||
req.SetFormData(form)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var res *resty.Response
|
||||
var err error
|
||||
var e Error
|
||||
req.SetError(&e)
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
case base.Delete:
|
||||
res, err = req.Delete(url)
|
||||
case base.Patch:
|
||||
res, err = req.Patch(url)
|
||||
case base.Put:
|
||||
res, err = req.Put(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, headers, query, form, data, resp, account)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&GoogleDrive{})
|
||||
}
|
@ -1,171 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type GoogleDrive struct{}
|
||||
|
||||
func (driver GoogleDrive) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "GoogleDrive",
|
||||
OnlyProxy: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Items() []Item {
|
||||
return []Item{
|
||||
{
|
||||
Name: "client_id",
|
||||
Label: "client id",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "client_secret",
|
||||
Label: "client secret",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "refresh_token",
|
||||
Label: "refresh token",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: "string",
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Save(account *model.Account, old *model.Account) error {
|
||||
account.Proxy = true
|
||||
err := driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "root"
|
||||
}
|
||||
account.Status = "work"
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []GoogleFile
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]GoogleFile)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if file.Type == conf.FOLDER {
|
||||
return "", NotFile
|
||||
}
|
||||
link := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id)
|
||||
var e GoogleError
|
||||
_, _ = googleClient.R().SetError(&e).
|
||||
SetHeader("Authorization", "Bearer "+account.AccessToken).
|
||||
Get(link)
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return "", err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
return "", fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
return link + "&alt=media", nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("google path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Proxy(c *gin.Context, account *model.Account) {
|
||||
c.Request.Header.Add("Authorization", "Bearer "+account.AccessToken)
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*GoogleDrive)(nil)
|
@ -1,157 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var googleClient = resty.New()
|
||||
|
||||
type GoogleTokenError struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) RefreshToken(account *model.Account) error {
|
||||
url := "https://www.googleapis.com/oauth2/v4/token"
|
||||
var resp TokenResp
|
||||
var e GoogleTokenError
|
||||
_, err := googleClient.R().SetResult(&resp).SetError(&e).
|
||||
SetFormData(map[string]string{
|
||||
"client_id": account.ClientId,
|
||||
"client_secret": account.ClientSecret,
|
||||
"refresh_token": account.RefreshToken,
|
||||
"grant_type": "refresh_token",
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Error != "" {
|
||||
return fmt.Errorf(e.Error)
|
||||
}
|
||||
account.AccessToken = resp.AccessToken
|
||||
account.Status = "work"
|
||||
return nil
|
||||
}
|
||||
|
||||
type GoogleFile struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
MimeType string `json:"mimeType"`
|
||||
ModifiedTime *time.Time `json:"modifiedTime"`
|
||||
Size string `json:"size"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) IsDir(mimeType string) bool {
|
||||
return mimeType == "application/vnd.google-apps.folder" || mimeType == "application/vnd.google-apps.shortcut"
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
|
||||
f := &model.File{
|
||||
Id: file.Id,
|
||||
Name: file.Name,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: file.ModifiedTime,
|
||||
Thumbnail: "",
|
||||
Url: "",
|
||||
}
|
||||
if driver.IsDir(file.MimeType) {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
size, _ := strconv.ParseInt(file.Size, 10, 64)
|
||||
f.Size = size
|
||||
f.Type = utils.GetFileType(filepath.Ext(file.Name))
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type GoogleFiles struct {
|
||||
NextPageToken string `json:"nextPageToken"`
|
||||
Files []GoogleFile `json:"files"`
|
||||
}
|
||||
|
||||
type GoogleError struct {
|
||||
Error struct {
|
||||
Errors []struct {
|
||||
Domain string `json:"domain"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
LocationType string `json:"location_type"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleFile, error) {
|
||||
pageToken := "first"
|
||||
res := make([]GoogleFile, 0)
|
||||
for pageToken != "" {
|
||||
if pageToken == "first" {
|
||||
pageToken = ""
|
||||
}
|
||||
var resp GoogleFiles
|
||||
var e GoogleError
|
||||
_, err := googleClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("Authorization", "Bearer "+account.AccessToken).
|
||||
SetQueryParams(map[string]string{
|
||||
"orderBy": "folder,name,modifiedTime desc",
|
||||
"fields": "files(id,name,mimeType,size,modifiedTime),nextPageToken",
|
||||
"pageSize": "1000",
|
||||
"q": fmt.Sprintf("'%s' in parents and trashed = false", id),
|
||||
"includeItemsFromAllDrives": "true",
|
||||
"supportsAllDrives": "true",
|
||||
"pageToken": pageToken,
|
||||
}).Get("https://www.googleapis.com/drive/v3/files")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.GetFiles(id, account)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
pageToken = resp.NextPageToken
|
||||
res = append(res, resp.Files...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//func (driver GoogleDrive) GetFile(path string, account *model.Account) (*GoogleFile, error) {
|
||||
// dir, name := filepath.Split(path)
|
||||
// dir = utils.ParsePath(dir)
|
||||
// _, _, err := driver.Path(dir, account)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
||||
// parentFiles, _ := parentFiles_.([]GoogleFile)
|
||||
// for _, file := range parentFiles {
|
||||
// if file.Name == name {
|
||||
// if !driver.IsDir(file.MimeType) {
|
||||
// return &file, err
|
||||
// } else {
|
||||
// return nil, drivers.NotFile
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil, drivers.PathNotFound
|
||||
//}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&GoogleDrive{})
|
||||
googleClient.SetRetryCount(3)
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package drivers
|
||||
package lanzou
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -12,48 +12,48 @@ import (
|
||||
|
||||
type Lanzou struct{}
|
||||
|
||||
func (driver Lanzou) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "Lanzou",
|
||||
OnlyProxy: false,
|
||||
func (driver Lanzou) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Lanzou",
|
||||
NoCors: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Lanzou) Items() []Item {
|
||||
return []Item{
|
||||
func (driver Lanzou) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "onedrive_type",
|
||||
Name: "internal_type",
|
||||
Label: "lanzou type",
|
||||
Type: SELECT,
|
||||
Type: base.TypeSelect,
|
||||
Required: true,
|
||||
Values: "cookie,url",
|
||||
},
|
||||
{
|
||||
Name: "access_token",
|
||||
Label: "cookie",
|
||||
Type: STRING,
|
||||
Type: base.TypeString,
|
||||
Description: "about 15 days valid",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: STRING,
|
||||
Type: base.TypeString,
|
||||
},
|
||||
{
|
||||
Name: "site_url",
|
||||
Label: "share url",
|
||||
Type: STRING,
|
||||
Type: base.TypeString,
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "share password",
|
||||
Type: STRING,
|
||||
Type: base.TypeString,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Lanzou) Save(account *model.Account, old *model.Account) error {
|
||||
if account.OnedriveType == "cookie" {
|
||||
if account.InternalType == "cookie" {
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "-1"
|
||||
}
|
||||
@ -85,13 +85,13 @@ func (driver Lanzou) File(path string, account *model.Account) (*model.File, err
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []LanZouFile
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]LanZouFile)
|
||||
} else {
|
||||
@ -104,7 +104,7 @@ func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, e
|
||||
return nil, err
|
||||
}
|
||||
if len(rawFiles) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||
_ = base.SetCache(path, rawFiles, account)
|
||||
}
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
@ -114,24 +114,27 @@ func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Lanzou) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.File(path, account)
|
||||
func (driver Lanzou) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("down file: %+v", file)
|
||||
downId := file.Id
|
||||
if account.OnedriveType == "cookie" {
|
||||
if account.InternalType == "cookie" {
|
||||
downId, err = driver.GetDownPageId(file.Id, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
link, err := driver.GetLink(downId)
|
||||
url, err := driver.GetLink(downId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return link, nil
|
||||
link := base.Link{
|
||||
Url: url,
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
@ -141,8 +144,7 @@ func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []m
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
file.Url, _ = driver.Link(path, account)
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -157,7 +159,27 @@ func (driver Lanzou) Proxy(c *gin.Context, account *model.Account) {
|
||||
}
|
||||
|
||||
func (driver Lanzou) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*Lanzou)(nil)
|
||||
func (driver *Lanzou) MakeDir(path string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver *Lanzou) Move(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver *Lanzou) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver *Lanzou) Delete(path string, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
func (driver *Lanzou) Upload(file *model.FileStream, account *model.Account) error {
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Lanzou)(nil)
|
@ -1,8 +1,9 @@
|
||||
package drivers
|
||||
package lanzou
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
@ -52,7 +53,7 @@ type LanZouFilesResp struct {
|
||||
}
|
||||
|
||||
func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZouFile, error) {
|
||||
if account.OnedriveType == "cookie" {
|
||||
if account.InternalType == "cookie" {
|
||||
files := make([]LanZouFile, 0)
|
||||
var resp LanZouFilesResp
|
||||
// folders
|
||||
@ -231,7 +232,7 @@ func (driver *Lanzou) GetLink(downId string) (string, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&Lanzou{})
|
||||
base.RegisterDriver(&Lanzou{})
|
||||
lanzouClient.
|
||||
SetRetryCount(3).
|
||||
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
@ -1,5 +0,0 @@
|
||||
package drivers
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&Native{})
|
||||
}
|
@ -1,12 +1,14 @@
|
||||
package drivers
|
||||
package native
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -15,35 +17,24 @@ import (
|
||||
|
||||
type Native struct{}
|
||||
|
||||
func (driver Native) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "Native",
|
||||
OnlyProxy: true,
|
||||
func (driver Native) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Native",
|
||||
OnlyProxy: true,
|
||||
OnlyLocal: true,
|
||||
NoNeedSetLink: true,
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Native) Items() []Item {
|
||||
return []Item{
|
||||
func (driver Native) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder path",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Values: "name,size,updated_at",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: "select",
|
||||
Values: "ASC,DESC",
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,7 +57,7 @@ func (driver Native) Save(account *model.Account, old *model.Account) error {
|
||||
func (driver Native) File(path string, account *model.Account) (*model.File, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
if !utils.Exists(fullPath) {
|
||||
return nil, PathNotFound
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
f, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
@ -90,7 +81,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err
|
||||
func (driver Native) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
if !utils.Exists(fullPath) {
|
||||
return nil, PathNotFound
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
rawFiles, err := ioutil.ReadDir(fullPath)
|
||||
@ -120,16 +111,19 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Native) Link(path string, account *model.Account) (string, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
func (driver Native) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, args.Path)
|
||||
s, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
if s.IsDir() {
|
||||
return "", fmt.Errorf("can't down folder")
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
return fullPath, nil
|
||||
link := base.Link{
|
||||
Url: fullPath,
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Native) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
@ -138,7 +132,7 @@ func (driver Native) Path(path string, account *model.Account) (*model.File, []m
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
if !file.IsDir() {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
@ -146,7 +140,7 @@ func (driver Native) Path(path string, account *model.Account) (*model.File, []m
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
model.SortFiles(files, account)
|
||||
//model.SortFiles(files, account)
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
@ -155,7 +149,77 @@ func (driver Native) Proxy(c *gin.Context, account *model.Account) {
|
||||
}
|
||||
|
||||
func (driver Native) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*Native)(nil)
|
||||
func (driver Native) MakeDir(path string, account *model.Account) error {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
err := os.MkdirAll(fullPath, 0700)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Native) Move(src string, dst string, account *model.Account) error {
|
||||
fullSrc := filepath.Join(account.RootFolder, src)
|
||||
fullDst := filepath.Join(account.RootFolder, dst)
|
||||
return os.Rename(fullSrc, fullDst)
|
||||
}
|
||||
|
||||
func (driver Native) Copy(src string, dst string, account *model.Account) error {
|
||||
fullSrc := filepath.Join(account.RootFolder, src)
|
||||
fullDst := filepath.Join(account.RootFolder, dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstFile, err := driver.File(dst, account)
|
||||
if err == nil {
|
||||
if !dstFile.IsDir() {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
}
|
||||
if srcFile.IsDir() {
|
||||
return driver.CopyDir(fullSrc, fullDst)
|
||||
}
|
||||
return driver.CopyFile(fullSrc, fullDst)
|
||||
}
|
||||
|
||||
func (driver Native) Delete(path string, account *model.Account) error {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if file.IsDir() {
|
||||
return os.RemoveAll(fullPath)
|
||||
}
|
||||
return os.Remove(fullPath)
|
||||
}
|
||||
|
||||
func (driver Native) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
fullPath := filepath.Join(account.RootFolder, file.ParentPath, file.Name)
|
||||
_, err := driver.File(filepath.Join(file.ParentPath, file.Name), account)
|
||||
if err == nil {
|
||||
// TODO overwrite?
|
||||
}
|
||||
basePath := filepath.Dir(fullPath)
|
||||
if !utils.Exists(basePath) {
|
||||
err := os.MkdirAll(basePath, 0744)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
out, err := os.Create(fullPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = out.Close()
|
||||
}()
|
||||
_, err = io.Copy(out, file)
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Native)(nil)
|
74
drivers/native/native.go
Normal file
74
drivers/native/native.go
Normal file
@ -0,0 +1,74 @@
|
||||
package native
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
// File copies a single file from src to dst
|
||||
func (driver *Native) CopyFile(src, dst string) error {
|
||||
var err error
|
||||
var srcfd *os.File
|
||||
var dstfd *os.File
|
||||
var srcinfo os.FileInfo
|
||||
|
||||
if srcfd, err = os.Open(src); err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcfd.Close()
|
||||
|
||||
if dstfd, err = os.Create(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstfd.Close()
|
||||
|
||||
if _, err = io.Copy(dstfd, srcfd); err != nil {
|
||||
return err
|
||||
}
|
||||
if srcinfo, err = os.Stat(src); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Chmod(dst, srcinfo.Mode())
|
||||
}
|
||||
|
||||
// Dir copies a whole directory recursively
|
||||
func (driver *Native) CopyDir(src string, dst string) error {
|
||||
var err error
|
||||
var fds []os.FileInfo
|
||||
var srcinfo os.FileInfo
|
||||
|
||||
if srcinfo, err = os.Stat(src); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fds, err = ioutil.ReadDir(src); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, fd := range fds {
|
||||
srcfp := path.Join(src, fd.Name())
|
||||
dstfp := path.Join(dst, fd.Name())
|
||||
|
||||
if fd.IsDir() {
|
||||
if err = driver.CopyDir(srcfp, dstfp); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
if err = driver.CopyFile(srcfp, dstfp); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Native{})
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package drivers
|
||||
package onedrive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -13,77 +14,77 @@ import (
|
||||
|
||||
type Onedrive struct{}
|
||||
|
||||
func (driver Onedrive) Config() DriverConfig {
|
||||
return DriverConfig{
|
||||
Name: "Onedrive",
|
||||
OnlyProxy: false,
|
||||
func (driver Onedrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Onedrive",
|
||||
NoNeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Onedrive) Items() []Item {
|
||||
return []Item{
|
||||
func (driver Onedrive) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "zone",
|
||||
Label: "zone",
|
||||
Type: "select",
|
||||
Type: base.TypeSelect,
|
||||
Required: true,
|
||||
Values: "global,cn,us,de",
|
||||
Description: "",
|
||||
},
|
||||
{
|
||||
Name: "onedrive_type",
|
||||
Name: "internal_type",
|
||||
Label: "onedrive type",
|
||||
Type: "select",
|
||||
Type: base.TypeSelect,
|
||||
Required: true,
|
||||
Values: "onedrive,sharepoint",
|
||||
},
|
||||
{
|
||||
Name: "client_id",
|
||||
Label: "client id",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "client_secret",
|
||||
Label: "client secret",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "redirect_uri",
|
||||
Label: "redirect uri",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "refresh_token",
|
||||
Label: "refresh token",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "site_id",
|
||||
Label: "site id",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder path",
|
||||
Type: "string",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Type: base.TypeSelect,
|
||||
Values: "name,size,lastModifiedDateTime",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: "select",
|
||||
Type: base.TypeSelect,
|
||||
Values: "asc,desc",
|
||||
Required: false,
|
||||
},
|
||||
@ -147,12 +148,12 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, PathNotFound
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ := cache.([]model.File)
|
||||
return files, nil
|
||||
@ -166,20 +167,23 @@ func (driver Onedrive) Files(path string, account *model.Account) ([]model.File,
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
if len(files) > 0 {
|
||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
|
||||
_ = base.SetCache(path, files, account)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Onedrive) Link(path string, account *model.Account) (string, error) {
|
||||
file, err := driver.GetFile(account, path)
|
||||
func (driver Onedrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.GetFile(account, args.Path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
if file.File.MimeType == "" {
|
||||
return "", fmt.Errorf("can't down folder")
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
return file.Url, nil
|
||||
link := base.Link{
|
||||
Url: file.Url,
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (driver Onedrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
@ -188,8 +192,7 @@ func (driver Onedrive) Path(path string, account *model.Account) (*model.File, [
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -204,7 +207,92 @@ func (driver Onedrive) Proxy(c *gin.Context, account *model.Account) {
|
||||
}
|
||||
|
||||
func (driver Onedrive) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, NotSupport
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
var _ Driver = (*Onedrive)(nil)
|
||||
func (driver Onedrive) MakeDir(path string, account *model.Account) error {
|
||||
url := driver.GetMetaUrl(account, false, utils.Dir(path)) + "/children"
|
||||
data := base.Json{
|
||||
"name": utils.Base(path),
|
||||
"folder": base.Json{},
|
||||
"@microsoft.graph.conflictBehavior": "rename",
|
||||
}
|
||||
_, err := driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) Move(src string, dst string, account *model.Account) error {
|
||||
log.Debugf("onedrive move")
|
||||
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"parentReference": base.Json{
|
||||
"id": dstParentFile.Id,
|
||||
},
|
||||
"name": utils.Base(dst),
|
||||
}
|
||||
url := driver.GetMetaUrl(account, false, src)
|
||||
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) Copy(src string, dst string, account *model.Account) error {
|
||||
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"parentReference": base.Json{
|
||||
"driveId": dstParentFile.ParentReference.DriveId,
|
||||
"id": dstParentFile.Id,
|
||||
},
|
||||
"name": utils.Base(dst),
|
||||
}
|
||||
url := driver.GetMetaUrl(account, false, src) + "/copy"
|
||||
_, err = driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) Delete(path string, account *model.Account) error {
|
||||
url := driver.GetMetaUrl(account, false, path)
|
||||
_, err := driver.Request(url, base.Delete, nil, nil, nil, nil, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
var err error
|
||||
if file.GetSize() <= 4*1024*1024 {
|
||||
err = driver.UploadSmall(file, account)
|
||||
} else {
|
||||
err = driver.UploadBig(file, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(file.ParentPath), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Onedrive)(nil)
|
@ -1,13 +1,21 @@
|
||||
package drivers
|
||||
package onedrive
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -44,7 +52,7 @@ func (driver Onedrive) GetMetaUrl(account *model.Account, auth bool, path string
|
||||
if auth {
|
||||
return host.Oauth
|
||||
}
|
||||
switch account.OnedriveType {
|
||||
switch account.InternalType {
|
||||
case "onedrive":
|
||||
{
|
||||
if path == "/" || path == "\\" {
|
||||
@ -72,8 +80,16 @@ type OneTokenErr struct {
|
||||
}
|
||||
|
||||
func (driver Onedrive) RefreshToken(account *model.Account) error {
|
||||
err := driver.refreshToken(account)
|
||||
if err != nil && err.Error() == "empty refresh_token" {
|
||||
return driver.refreshToken(account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) refreshToken(account *model.Account) error {
|
||||
url := driver.GetMetaUrl(account, true, "") + "/common/oauth2/v2.0/token"
|
||||
var resp TokenResp
|
||||
var resp base.TokenResp
|
||||
var e OneTokenErr
|
||||
_, err := oneClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{
|
||||
"grant_type": "refresh_token",
|
||||
@ -92,11 +108,16 @@ func (driver Onedrive) RefreshToken(account *model.Account) error {
|
||||
} else {
|
||||
account.Status = "work"
|
||||
}
|
||||
if resp.RefreshToken == "" {
|
||||
account.Status = "empty refresh_token"
|
||||
return errors.New("empty refresh_token")
|
||||
}
|
||||
account.RefreshToken, account.AccessToken = resp.RefreshToken, resp.AccessToken
|
||||
return nil
|
||||
}
|
||||
|
||||
type OneFile struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
LastModifiedDateTime *time.Time `json:"lastModifiedDateTime"`
|
||||
@ -104,11 +125,14 @@ type OneFile struct {
|
||||
File struct {
|
||||
MimeType string `json:"mimeType"`
|
||||
} `json:"file"`
|
||||
Thumbnails []struct{
|
||||
Medium struct{
|
||||
Thumbnails []struct {
|
||||
Medium struct {
|
||||
Url string `json:"url"`
|
||||
} `json:"medium"`
|
||||
} `json:"thumbnails"`
|
||||
ParentReference struct {
|
||||
DriveId string `json:"driveId"`
|
||||
} `json:"parentReference"`
|
||||
}
|
||||
|
||||
type OneFiles struct {
|
||||
@ -130,6 +154,7 @@ func (driver Onedrive) FormatFile(file *OneFile) *model.File {
|
||||
UpdatedAt: file.LastModifiedDateTime,
|
||||
Driver: driver.Config().Name,
|
||||
Url: file.Url,
|
||||
Id: file.Id,
|
||||
}
|
||||
if len(file.Thumbnails) > 0 {
|
||||
f.Thumbnail = file.Thumbnails[0].Medium.Url
|
||||
@ -184,7 +209,110 @@ func (driver Onedrive) GetFile(account *model.Account, path string) (*OneFile, e
|
||||
return &file, nil
|
||||
}
|
||||
|
||||
func (driver Onedrive) Request(url string, method int, headers, query, form map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if form != nil {
|
||||
req.SetFormData(form)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var res *resty.Response
|
||||
var err error
|
||||
var e OneRespErr
|
||||
req.SetError(&e)
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
case base.Patch:
|
||||
res, err = req.Patch(url)
|
||||
case base.Delete:
|
||||
res, err = req.Delete(url)
|
||||
case base.Put:
|
||||
res, err = req.Put(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Error.Code != "" {
|
||||
if e.Error.Code == "InvalidAuthenticationToken" {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, headers, query, form, data, resp, account)
|
||||
}
|
||||
return nil, errors.New(e.Error.Message)
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func (driver Onedrive) UploadSmall(file *model.FileStream, account *model.Account) error {
|
||||
url := driver.GetMetaUrl(account, false, utils.Join(file.ParentPath, file.Name)) + "/content"
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = driver.Request(url, base.Put, nil, nil, nil, data, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Onedrive) UploadBig(file *model.FileStream, account *model.Account) error {
|
||||
url := driver.GetMetaUrl(account, false, utils.Join(file.ParentPath, file.Name)) + "/createUploadSession"
|
||||
res, err := driver.Request(url, base.Post, nil, nil, nil, nil, nil, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uploadUrl := jsoniter.Get(res, "uploadUrl").ToString()
|
||||
var finish uint64 = 0
|
||||
const DEFAULT = 4 * 1024 * 1024
|
||||
for finish < file.GetSize() {
|
||||
log.Debugf("upload: %d", finish)
|
||||
var byteSize uint64 = DEFAULT
|
||||
left := file.GetSize() - finish
|
||||
if left < DEFAULT {
|
||||
byteSize = left
|
||||
}
|
||||
byteData := make([]byte, byteSize)
|
||||
n, err := io.ReadFull(file, byteData)
|
||||
log.Debug(err, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", uploadUrl, bytes.NewBuffer(byteData))
|
||||
req.Header.Set("Content-Length", strconv.Itoa(int(byteSize)))
|
||||
req.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", finish, finish+byteSize-1, file.Size))
|
||||
finish += byteSize
|
||||
res, err := base.HttpClient.Do(req)
|
||||
if res.StatusCode != 201 && res.StatusCode != 202 {
|
||||
data, _ := ioutil.ReadAll(res.Body)
|
||||
return errors.New(string(data))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterDriver(&Onedrive{})
|
||||
base.RegisterDriver(&Onedrive{})
|
||||
oneClient.SetRetryCount(3)
|
||||
}
|
||||
}
|
283
drivers/pikpak/driver.go
Normal file
283
drivers/pikpak/driver.go
Normal file
@ -0,0 +1,283 @@
|
||||
package pikpak
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"github.com/gin-gonic/gin"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PikPak struct{}
|
||||
|
||||
func (driver PikPak) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "PikPak",
|
||||
ApiProxy: true,
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver PikPak) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder id",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver PikPak) Save(account *model.Account, old *model.Account) error {
|
||||
err := driver.Login(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver PikPak) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver PikPak) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var files []model.File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ = cache.([]model.File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err := driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
if len(files) > 0 {
|
||||
_ = base.SetCache(path, files, account)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp File
|
||||
_, err = driver.Request(fmt.Sprintf("https://api-drive.mypikpak.com/drive/v1/files/%s?_magic=2021&thumbnail_size=SIZE_LARGE", file.Id),
|
||||
base.Get, nil, nil, &resp, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &base.Link{
|
||||
Url: resp.WebContentLink,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("pikpak path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Proxy(c *gin.Context, account *model.Account) {
|
||||
|
||||
}
|
||||
|
||||
func (driver PikPak) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver PikPak) MakeDir(path string, account *model.Account) error {
|
||||
path = utils.ParsePath(path)
|
||||
dir, name := filepath.Split(path)
|
||||
parentFile, err := driver.File(dir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files", base.Post, nil, &base.Json{
|
||||
"kind": "drive#folder",
|
||||
"parent_id": parentFile.Id,
|
||||
"name": name,
|
||||
}, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver PikPak) Move(src string, dst string, account *model.Account) error {
|
||||
srcDir, _ := filepath.Split(src)
|
||||
dstDir, dstName := filepath.Split(dst)
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// rename
|
||||
if srcDir == dstDir {
|
||||
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files/"+srcFile.Id, base.Patch, nil, &base.Json{
|
||||
"name": dstName,
|
||||
}, nil, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files:batchMove", base.Post, nil, &base.Json{
|
||||
"ids": []string{srcFile.Id},
|
||||
"to": base.Json{
|
||||
"parent_id": dstDirFile.Id,
|
||||
},
|
||||
}, nil, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver PikPak) Copy(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstDirFile, err := driver.File(utils.Dir(dst), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files:batchCopy", base.Post, nil, &base.Json{
|
||||
"ids": []string{srcFile.Id},
|
||||
"to": base.Json{
|
||||
"parent_id": dstDirFile.Id,
|
||||
},
|
||||
}, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver PikPak) Delete(path string, account *model.Account) error {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files:batchTrash", base.Post, nil, &base.Json{
|
||||
"ids": []string{file.Id},
|
||||
}, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver PikPak) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"kind": "drive#file",
|
||||
"name": file.GetFileName(),
|
||||
"size": file.GetSize(),
|
||||
"hash": "1CF254FBC456E1B012CD45C546636AA62CF8350E",
|
||||
"upload_type": "UPLOAD_TYPE_RESUMABLE",
|
||||
"objProvider": base.Json{"provider": "UPLOAD_TYPE_UNKNOWN"},
|
||||
"parent_id": parentFile.Id,
|
||||
}
|
||||
res, err := driver.Request("https://api-drive.mypikpak.com/drive/v1/files", base.Post, nil, &data, nil, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params := jsoniter.Get(res, "resumable").Get("params")
|
||||
endpoint := params.Get("endpoint").ToString()
|
||||
endpointS := strings.Split(endpoint, ".")
|
||||
endpoint = strings.Join(endpointS[1:], ".")
|
||||
accessKeyId := params.Get("access_key_id").ToString()
|
||||
accessKeySecret := params.Get("access_key_secret").ToString()
|
||||
securityToken := params.Get("security_token").ToString()
|
||||
client, err := oss.New("https://"+endpoint, accessKeyId,
|
||||
accessKeySecret, oss.SecurityToken(securityToken))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bucket, err := client.Bucket(params.Get("bucket").ToString())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
signedURL, err := bucket.SignURL(params.Get("key").ToString(), oss.HTTPPut, 60)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = bucket.PutObjectWithURL(signedURL, file)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*PikPak)(nil)
|
201
drivers/pikpak/pikpak.go
Normal file
201
drivers/pikpak/pikpak.go
Normal file
@ -0,0 +1,201 @@
|
||||
package pikpak
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RespErr struct {
|
||||
ErrorCode int `json:"error_code"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func (driver PikPak) Login(account *model.Account) error {
|
||||
url := "https://user.mypikpak.com/v1/auth/signin"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var e RespErr
|
||||
res, err := base.RestyClient.R().SetError(&e).SetBody(base.Json{
|
||||
"captcha_token": "",
|
||||
"client_id": "YNxT9w7GMdWvEOKa",
|
||||
"client_secret": "dbw2OtmVEeuUvIptb1Coyg",
|
||||
"username": account.Username,
|
||||
"password": account.Password,
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.ErrorCode != 0 {
|
||||
account.Status = e.Error
|
||||
err = errors.New(e.Error)
|
||||
} else {
|
||||
data := res.Body()
|
||||
account.Status = "work"
|
||||
account.RefreshToken = jsoniter.Get(data, "refresh_token").ToString()
|
||||
account.AccessToken = jsoniter.Get(data, "access_token").ToString()
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver PikPak) RefreshToken(account *model.Account) error {
|
||||
url := "https://user.mypikpak.com/v1/auth/token"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var e RespErr
|
||||
res, err := base.RestyClient.R().SetError(&e).
|
||||
SetHeader("user-agent", "").SetBody(base.Json{
|
||||
"client_id": "YNxT9w7GMdWvEOKa",
|
||||
"client_secret": "dbw2OtmVEeuUvIptb1Coyg",
|
||||
"grant_type": "refresh_token",
|
||||
"refresh_token": account.RefreshToken,
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
return err
|
||||
}
|
||||
if e.ErrorCode != 0 {
|
||||
if e.ErrorCode == 4126 {
|
||||
// refresh_token 失效,重新登陆
|
||||
return driver.Login(account)
|
||||
}
|
||||
account.Status = e.Error
|
||||
_ = model.SaveAccount(account)
|
||||
return errors.New(e.Error)
|
||||
}
|
||||
data := res.Body()
|
||||
account.Status = "work"
|
||||
account.RefreshToken = jsoniter.Get(data, "refresh_token").ToString()
|
||||
account.AccessToken = jsoniter.Get(data, "access_token").ToString()
|
||||
log.Debugf("%s\n %+v", res.String(), account)
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var e RespErr
|
||||
req.SetError(&e)
|
||||
var res *resty.Response
|
||||
var err error
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
case base.Patch:
|
||||
res, err = req.Patch(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.ErrorCode != 0 {
|
||||
if e.ErrorCode == 16 {
|
||||
// login / refresh token
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, query, data, resp, account)
|
||||
} else {
|
||||
return nil, errors.New(e.Error)
|
||||
}
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Id string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
ModifiedTime *time.Time `json:"modified_time"`
|
||||
Size string `json:"size"`
|
||||
ThumbnailLink string `json:"thumbnail_link"`
|
||||
WebContentLink string `json:"web_content_link"`
|
||||
}
|
||||
|
||||
func (driver PikPak) FormatFile(file *File) *model.File {
|
||||
size, _ := strconv.ParseInt(file.Size, 10, 64)
|
||||
f := &model.File{
|
||||
Id: file.Id,
|
||||
Name: file.Name,
|
||||
Size: size,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: file.ModifiedTime,
|
||||
Thumbnail: file.ThumbnailLink,
|
||||
}
|
||||
if file.Kind == "drive#folder" {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
f.Type = utils.GetFileType(filepath.Ext(file.Name))
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type Files struct {
|
||||
Files []File `json:"files"`
|
||||
NextPageToken string `json:"next_page_token"`
|
||||
}
|
||||
|
||||
func (driver PikPak) GetFiles(id string, account *model.Account) ([]File, error) {
|
||||
res := make([]File, 0)
|
||||
pageToken := "first"
|
||||
for pageToken != "" {
|
||||
if pageToken == "first" {
|
||||
pageToken = ""
|
||||
}
|
||||
query := map[string]string{
|
||||
"parent_id": id,
|
||||
"thumbnail_size": "SIZE_LARGE",
|
||||
"with_audit": "true",
|
||||
"limit": "100",
|
||||
"filters": `{"phase":{"eq":"PHASE_TYPE_COMPLETE"},"trashed":{"eq":false}}`,
|
||||
"page_token": pageToken,
|
||||
}
|
||||
var resp Files
|
||||
_, err := driver.Request("https://api-drive.mypikpak.com/drive/v1/files", base.Get, query, nil, &resp, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("%+v", resp)
|
||||
pageToken = resp.NextPageToken
|
||||
res = append(res, resp.Files...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&PikPak{})
|
||||
}
|
262
drivers/s3/driver.go
Normal file
262
drivers/s3/driver.go
Normal file
@ -0,0 +1,262 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type S3 struct {
|
||||
}
|
||||
|
||||
func (driver S3) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "S3",
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver S3) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "bucket",
|
||||
Label: "Bucket",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "endpoint",
|
||||
Label: "Endpoint",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "region",
|
||||
Label: "Region",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "access_key",
|
||||
Label: "Access Key",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "access_secret",
|
||||
Label: "Access Secret",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder path",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "custom_host",
|
||||
Label: "Custom Host",
|
||||
Type: base.TypeString,
|
||||
},
|
||||
{
|
||||
Name: "limit",
|
||||
Label: "url expire time(hours)",
|
||||
Type: base.TypeNumber,
|
||||
Description: "default 4 hours",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver S3) Save(account *model.Account, old *model.Account) error {
|
||||
if account.Limit == 0 {
|
||||
account.Limit = 4
|
||||
}
|
||||
client, err := driver.NewSession(account)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
} else {
|
||||
sessionsMap[account.Name] = client
|
||||
account.Status = "work"
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver S3) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver S3) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var files []model.File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ = cache.([]model.File)
|
||||
} else {
|
||||
files, err = driver.List(path, account)
|
||||
if err == nil && len(files) > 0 {
|
||||
_ = base.SetCache(path, files, account)
|
||||
}
|
||||
}
|
||||
return files, err
|
||||
}
|
||||
|
||||
func (driver S3) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
client, err := driver.GetClient(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := strings.TrimPrefix(args.Path, "/")
|
||||
disposition := fmt.Sprintf(`attachment;filename="%s"`, url.QueryEscape(utils.Base(path)))
|
||||
input := &s3.GetObjectInput{
|
||||
Bucket: &account.Bucket,
|
||||
Key: &path,
|
||||
ResponseContentDisposition: &disposition,
|
||||
}
|
||||
req, _ := client.GetObjectRequest(input)
|
||||
link, err := req.Presign(time.Hour * time.Duration(account.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &base.Link{
|
||||
Url: link,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (driver S3) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("s3 path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver S3) Proxy(c *gin.Context, account *model.Account) {
|
||||
|
||||
}
|
||||
|
||||
func (driver S3) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver S3) MakeDir(path string, account *model.Account) error {
|
||||
// not support, default as success
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver S3) Move(src string, dst string, account *model.Account) error {
|
||||
err := driver.Copy(src, dst, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return driver.Delete(src, account)
|
||||
}
|
||||
|
||||
func (driver S3) Copy(src string, dst string, account *model.Account) error {
|
||||
client, err := driver.GetClient(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srcKey := driver.GetKey(src, account, srcFile.IsDir())
|
||||
dstKey := driver.GetKey(dst, account, srcFile.IsDir())
|
||||
input := &s3.CopyObjectInput{
|
||||
Bucket: &account.Bucket,
|
||||
CopySource: &srcKey,
|
||||
Key: &dstKey,
|
||||
}
|
||||
_, err = client.CopyObject(input)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dst, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver S3) Delete(path string, account *model.Account) error {
|
||||
client, err := driver.GetClient(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := driver.GetKey(path, account, file.IsDir())
|
||||
input := &s3.DeleteObjectInput{
|
||||
Bucket: &account.Bucket,
|
||||
Key: &key,
|
||||
}
|
||||
_, err = client.DeleteObject(input)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver S3) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
s, ok := sessionsMap[account.Name]
|
||||
if !ok {
|
||||
return fmt.Errorf("can't find [%s] session", account.Name)
|
||||
}
|
||||
uploader := s3manager.NewUploader(s)
|
||||
key := driver.GetKey(utils.Join(file.ParentPath, file.GetFileName()), account, false)
|
||||
input := &s3manager.UploadInput{
|
||||
Bucket: &account.Bucket,
|
||||
Key: &key,
|
||||
Body: file,
|
||||
}
|
||||
_, err := uploader.Upload(input)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*S3)(nil)
|
118
drivers/s3/s3.go
Normal file
118
drivers/s3/s3.go
Normal file
@ -0,0 +1,118 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/request"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var sessionsMap map[string]*session.Session
|
||||
|
||||
func (driver S3) NewSession(account *model.Account) (*session.Session, error) {
|
||||
cfg := &aws.Config{
|
||||
Credentials: credentials.NewStaticCredentials(account.AccessKey, account.AccessSecret, ""),
|
||||
Region: &account.Region,
|
||||
Endpoint: &account.Endpoint,
|
||||
}
|
||||
return session.NewSession(cfg)
|
||||
}
|
||||
|
||||
func (driver S3) GetClient(account *model.Account) (*s3.S3, error) {
|
||||
s, ok := sessionsMap[account.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can't find [%s] session", account.Name)
|
||||
}
|
||||
client := s3.New(s)
|
||||
if account.CustomHost != "" {
|
||||
cURL, err := url.Parse(account.CustomHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client.Handlers.Build.PushBack(func(r *request.Request) {
|
||||
if r.HTTPRequest.Method != http.MethodGet {
|
||||
return
|
||||
}
|
||||
r.HTTPRequest.URL.Scheme = cURL.Scheme
|
||||
r.HTTPRequest.URL.Host = cURL.Host
|
||||
})
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (driver S3) List(prefix string, account *model.Account) ([]model.File, error) {
|
||||
prefix = driver.GetKey(prefix, account, true)
|
||||
log.Debugf("list: %s", prefix)
|
||||
client, err := driver.GetClient(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
marker := ""
|
||||
for {
|
||||
input := &s3.ListObjectsInput{
|
||||
Bucket: &account.Bucket,
|
||||
Marker: &marker,
|
||||
Prefix: &prefix,
|
||||
Delimiter: aws.String("/"),
|
||||
}
|
||||
listObjectsResult, err := client.ListObjects(input)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, object := range listObjectsResult.CommonPrefixes {
|
||||
file := model.File{
|
||||
//Id: *object.Key,
|
||||
Name: utils.Base(strings.Trim(*object.Prefix, "/")),
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
TimeStr: "-",
|
||||
Type: conf.FOLDER,
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
for _, object := range listObjectsResult.Contents {
|
||||
file := model.File{
|
||||
//Id: *object.Key,
|
||||
Name: utils.Base(*object.Key),
|
||||
Size: *object.Size,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: object.LastModified,
|
||||
Type: utils.GetFileType(path.Ext(*object.Key)),
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
if *listObjectsResult.IsTruncated {
|
||||
marker = *listObjectsResult.NextMarker
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver S3) GetKey(path string, account *model.Account, dir bool) string {
|
||||
path = utils.Join(account.RootFolder, path)
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
if path != "" && dir {
|
||||
path += "/"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func init() {
|
||||
sessionsMap = make(map[string]*session.Session, 0)
|
||||
base.RegisterDriver(&S3{})
|
||||
}
|
281
drivers/shandian/driver.go
Normal file
281
drivers/shandian/driver.go
Normal file
@ -0,0 +1,281 @@
|
||||
package shandian
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Shandian struct{}
|
||||
|
||||
func (driver Shandian) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "ShandianPan",
|
||||
NoNeedSetLink: true,
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Shandian) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account username/phone number",
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "account password",
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Shandian) Save(account *model.Account, old *model.Account) error {
|
||||
if account.RootFolder == "" {
|
||||
account.RootFolder = "0"
|
||||
}
|
||||
return driver.Login(account)
|
||||
}
|
||||
|
||||
func (driver Shandian) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Shandian) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var files []model.File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ = cache.([]model.File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawFiles, err := driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = make([]model.File, 0)
|
||||
for _, file := range rawFiles {
|
||||
files = append(files, *driver.FormatFile(&file))
|
||||
}
|
||||
if len(files) > 0 {
|
||||
_ = base.SetCache(path, files, account)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Shandian) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
log.Debugf("shandian link")
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var e Resp
|
||||
res, err := base.NoRedirectClient.R().SetError(&e).SetHeader("Accept", "application/json").SetQueryParams(map[string]string{
|
||||
"id": file.Id,
|
||||
"token": account.AccessToken,
|
||||
}).Get("https://shandianpan.com/api/pan/file-download")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Code != 0 {
|
||||
if e.Code == 10 {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Link(args, account)
|
||||
}
|
||||
return nil, errors.New(e.Msg)
|
||||
}
|
||||
return &base.Link{
|
||||
Url: res.Header().Get("location"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (driver Shandian) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("shandian path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver Shandian) Proxy(c *gin.Context, account *model.Account) {
|
||||
|
||||
}
|
||||
|
||||
func (driver Shandian) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Shandian) MakeDir(path string, account *model.Account) error {
|
||||
parentFile, err := driver.File(utils.Dir(path), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": parentFile.Id,
|
||||
"name": utils.Base(path),
|
||||
}
|
||||
_, err = driver.Post("https://shandianpan.com/api/pan/mkdir", data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Shandian) Move(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if utils.Dir(src) == utils.Dir(dst) {
|
||||
// rename
|
||||
data := map[string]interface{}{
|
||||
"id": srcFile.Id,
|
||||
"name": utils.Base(dst),
|
||||
}
|
||||
_, err = driver.Post("https://shandianpan.com/api/pan/change", data, nil, account)
|
||||
} else {
|
||||
dstParentFile, err := driver.File(utils.Dir(dst), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": srcFile.Id,
|
||||
"to_id": dstParentFile.Id,
|
||||
}
|
||||
_, err = driver.Post("https://shandianpan.com/api/pan/move", data, nil, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Shandian) Copy(src string, dst string, account *model.Account) error {
|
||||
return base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Shandian) Delete(path string, account *model.Account) error {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": file.Id,
|
||||
}
|
||||
_, err = driver.Post("https://shandianpan.com/api/pan/recycle-in", data, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Shandian) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var resp UploadResp
|
||||
parentId, err := strconv.Atoi(parentFile.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"id": parentId,
|
||||
"name": file.GetFileName(),
|
||||
}
|
||||
_, err = driver.Post("https://shandianpan.com/api/pan/upload", data, &resp, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
if resp.Code == 10 {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return driver.Upload(file, account)
|
||||
}
|
||||
return errors.New(resp.Msg)
|
||||
}
|
||||
var r Resp
|
||||
_, err = base.RestyClient.R().SetMultipartFormData(map[string]string{
|
||||
"token": account.AccessToken,
|
||||
"id": "0",
|
||||
"key": resp.Data.Key,
|
||||
"ossAccessKeyId": resp.Data.Accessid,
|
||||
"policy": resp.Data.Policy,
|
||||
"signature": resp.Data.Signature,
|
||||
"callback": resp.Data.Callback,
|
||||
}).SetMultipartField("file", file.GetFileName(), file.GetMIMEType(), file).
|
||||
SetResult(&r).SetError(&r).Post("https:" + resp.Data.Host + "/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r.Code == 0 {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
return nil
|
||||
}
|
||||
return errors.New(r.Msg)
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Shandian)(nil)
|
150
drivers/shandian/shandian.go
Normal file
150
drivers/shandian/shandian.go
Normal file
@ -0,0 +1,150 @@
|
||||
package shandian
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Resp struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type LoginResp struct {
|
||||
Resp
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (driver Shandian) Login(account *model.Account) error {
|
||||
var resp LoginResp
|
||||
_, err := base.RestyClient.R().SetResult(&resp).SetHeader("Accept", "application/json").SetBody(base.Json{
|
||||
"mobile": account.Username,
|
||||
"password": account.Password,
|
||||
"smscode": "",
|
||||
}).Post("https://shandianpan.com/api/login")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
account.Status = resp.Msg
|
||||
err = errors.New(resp.Msg)
|
||||
} else {
|
||||
account.Status = "work"
|
||||
account.AccessToken = resp.Data.Token
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Id int64 `json:"id"`
|
||||
Type int `json:"type"`
|
||||
Name string `json:"name"`
|
||||
UpdateTime int64 `json:"update_time"`
|
||||
Size int64 `json:"size"`
|
||||
Ext string `json:"ext"`
|
||||
}
|
||||
|
||||
func (driver Shandian) FormatFile(file *File) *model.File {
|
||||
t := time.Unix(file.UpdateTime, 0)
|
||||
f := &model.File{
|
||||
Id: strconv.FormatInt(file.Id, 10),
|
||||
Name: file.Name,
|
||||
Size: file.Size,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: &t,
|
||||
}
|
||||
if file.Type == 1 {
|
||||
f.Type = conf.FOLDER
|
||||
} else {
|
||||
f.Type = utils.GetFileType(file.Ext)
|
||||
if file.Ext != "" {
|
||||
f.Name += "." + file.Ext
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (driver Shandian) Post(url string, data map[string]interface{}, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Accept", "application/json")
|
||||
data["token"] = account.AccessToken
|
||||
req.SetBody(data)
|
||||
var e Resp
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
} else {
|
||||
req.SetResult(&e)
|
||||
}
|
||||
req.SetError(&e)
|
||||
res, err := req.Post(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Code != 0 {
|
||||
if e.Code == 10 {
|
||||
err = driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Post(url, data, resp, account)
|
||||
}
|
||||
return nil, errors.New(e.Msg)
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
type FilesResp struct {
|
||||
Resp
|
||||
Data []File `json:"data"`
|
||||
}
|
||||
|
||||
func (driver Shandian) GetFiles(id string, account *model.Account) ([]File, error) {
|
||||
// TODO page not wok
|
||||
//res := make([]File, 0)
|
||||
page := 1
|
||||
//for {
|
||||
data := map[string]interface{}{
|
||||
"id": id,
|
||||
"page": page,
|
||||
"page_size": 100,
|
||||
}
|
||||
var resp FilesResp
|
||||
_, err := driver.Post("https://shandianpan.com/api/pan", data, &resp, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//res = append(res, resp.Data...)
|
||||
// if len(resp.Data) == 0 {
|
||||
// break
|
||||
// }
|
||||
//}
|
||||
//return res, nil
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
type UploadResp struct {
|
||||
Resp
|
||||
Data struct {
|
||||
Accessid string `json:"accessid"`
|
||||
Policy string `json:"policy"`
|
||||
Expire int `json:"expire"`
|
||||
Callback string `json:"callback"`
|
||||
Key string `json:"key"`
|
||||
Host string `json:"host"`
|
||||
Signature string `json:"signature"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Shandian{})
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package drivers
|
||||
|
||||
import "fmt"
|
||||
|
||||
var (
|
||||
PathNotFound = fmt.Errorf("path not found")
|
||||
NotFile = fmt.Errorf("not file")
|
||||
NotImplement = fmt.Errorf("not implement")
|
||||
NotSupport = fmt.Errorf("not support")
|
||||
)
|
||||
|
||||
const (
|
||||
STRING = "string"
|
||||
SELECT = "select"
|
||||
BOOL = "bool"
|
||||
)
|
197
drivers/webdav/driver.go
Normal file
197
drivers/webdav/driver.go
Normal file
@ -0,0 +1,197 @@
|
||||
package webdav
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type WebDav struct{}
|
||||
|
||||
func (driver WebDav) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "WebDav",
|
||||
OnlyProxy: true,
|
||||
OnlyLocal: true,
|
||||
NoNeedSetLink: true,
|
||||
LocalSort: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (driver WebDav) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "site_url",
|
||||
Label: "webdav root url",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "username",
|
||||
Label: "username",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Label: "password",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver WebDav) Save(account *model.Account, old *model.Account) error {
|
||||
account.Status = "work"
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver WebDav) File(path string, account *model.Account) (*model.File, error) {
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: "/",
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver WebDav) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ := cache.([]model.File)
|
||||
return files, nil
|
||||
}
|
||||
c := driver.NewClient(account)
|
||||
rawFiles, err := c.ReadDir(driver.WebDavPath(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := make([]model.File, 0)
|
||||
if len(rawFiles) == 0 {
|
||||
return files, nil
|
||||
}
|
||||
for _, f := range rawFiles {
|
||||
t := f.ModTime()
|
||||
file := model.File{
|
||||
Name: f.Name(),
|
||||
Size: f.Size(),
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: &t,
|
||||
}
|
||||
if f.IsDir() {
|
||||
file.Type = conf.FOLDER
|
||||
} else {
|
||||
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
_ = base.SetCache(path, files, account)
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver WebDav) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
c := driver.NewClient(account)
|
||||
reader, err := c.ReadStream(driver.WebDavPath(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &base.Link{Data: reader}, nil
|
||||
}
|
||||
|
||||
func (driver WebDav) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
func (driver WebDav) Proxy(c *gin.Context, account *model.Account) {
|
||||
|
||||
}
|
||||
|
||||
func (driver WebDav) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver WebDav) MakeDir(path string, account *model.Account) error {
|
||||
c := driver.NewClient(account)
|
||||
err := c.MkdirAll(driver.WebDavPath(path), 0644)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver WebDav) Move(src string, dst string, account *model.Account) error {
|
||||
c := driver.NewClient(account)
|
||||
err := c.Rename(driver.WebDavPath(src), driver.WebDavPath(dst), true)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(src), account)
|
||||
if utils.Dir(src) != utils.Dir(dst) {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver WebDav) Copy(src string, dst string, account *model.Account) error {
|
||||
c := driver.NewClient(account)
|
||||
err := c.Copy(driver.WebDavPath(src), driver.WebDavPath(dst), true)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(dst), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver WebDav) Delete(path string, account *model.Account) error {
|
||||
c := driver.NewClient(account)
|
||||
err := c.RemoveAll(driver.WebDavPath(path))
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver WebDav) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
c := driver.NewClient(account)
|
||||
path := utils.Join(file.ParentPath, file.Name)
|
||||
err := c.WriteStream(driver.WebDavPath(path), file, 0644)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(file.ParentPath), account)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ base.Driver = (*WebDav)(nil)
|
23
drivers/webdav/webdav.go
Normal file
23
drivers/webdav/webdav.go
Normal file
@ -0,0 +1,23 @@
|
||||
package webdav
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/studio-b12/gowebdav"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (driver WebDav) NewClient(account *model.Account) *gowebdav.Client {
|
||||
return gowebdav.NewClient(account.SiteUrl, account.Username, account.Password)
|
||||
}
|
||||
|
||||
func (driver WebDav) WebDavPath(path string) string {
|
||||
path = utils.ParsePath(path)
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
return path
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&WebDav{})
|
||||
}
|
16
go.mod
16
go.mod
@ -3,13 +3,17 @@ module github.com/Xhofe/alist
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go v1.27.0
|
||||
github.com/eko/gocache/v2 v2.1.0
|
||||
github.com/gin-contrib/cors v1.3.1
|
||||
github.com/gin-gonic/gin v1.7.4
|
||||
github.com/go-playground/validator/v10 v10.9.0
|
||||
github.com/go-resty/resty/v2 v2.6.0
|
||||
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/robfig/cron/v3 v3.0.0
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
golang.org/x/text v0.3.7
|
||||
gorm.io/driver/mysql v1.1.2
|
||||
gorm.io/driver/postgres v1.1.2
|
||||
gorm.io/driver/sqlite v1.1.6
|
||||
@ -18,15 +22,16 @@ require (
|
||||
|
||||
require (
|
||||
github.com/XiaoMi/pegasus-go-client v0.0.0-20210427083443-f3b6b08bc4c2 // indirect
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/gin-contrib/cors v1.3.1 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.9.0 // indirect
|
||||
github.com/go-redis/redis/v8 v8.9.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
@ -40,7 +45,7 @@ require (
|
||||
github.com/jackc/pgx/v4 v4.13.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.9 // indirect
|
||||
@ -53,14 +58,15 @@ require (
|
||||
github.com/prometheus/common v0.18.0 // indirect
|
||||
github.com/prometheus/procfs v0.6.0 // indirect
|
||||
github.com/spf13/cast v1.3.1 // indirect
|
||||
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f // indirect
|
||||
github.com/ugorji/go/codec v1.2.6 // indirect
|
||||
go.opentelemetry.io/otel v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v0.20.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
|
||||
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
|
||||
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
|
||||
google.golang.org/protobuf v1.27.1 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
|
||||
|
13
go.sum
13
go.sum
@ -20,6 +20,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible h1:ht2+VfbXtNLGhCsnTMc6/N26nSTBK6qdhktjYyjJQkk=
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
|
||||
github.com/allegro/bigcache/v2 v2.2.5 h1:mRc8r6GQjuJsmSKQNPsR5jQVXc8IJ1xsW5YXUYMLfqI=
|
||||
github.com/allegro/bigcache/v2 v2.2.5/go.mod h1:FppZsIO+IZk7gCuj5FiIDHGygD9xvWQcqg1uIPMb6tY=
|
||||
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
|
||||
@ -29,6 +31,7 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
|
||||
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
|
||||
github.com/aws/aws-sdk-go v1.27.0 h1:0xphMHGMLBrPMfxR2AmVjZKcMEESEgWF8Kru94BNByk=
|
||||
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
|
||||
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
@ -266,6 +269,9 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
|
||||
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b h1:Ur6QAxsHCK99Quj9PaWafoV4unb0DO/HWiKExD+TN5g=
|
||||
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
|
||||
@ -482,6 +488,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f h1:L2NE7BXnSlSLoNYZ0lCwZDjdnYjCNYC71k9ClZUTFTs=
|
||||
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=
|
||||
@ -567,8 +575,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
|
||||
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211209124913-491a49abca63 h1:iocB37TsdFuN6IBRZ+ry36wrkoV51/tl5vOWqkcPGvY=
|
||||
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -632,6 +640,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
|
@ -7,22 +7,21 @@ import (
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
Name string `json:"name" gorm:"unique" binding:"required"`
|
||||
Index int `json:"index"`
|
||||
Type string `json:"type"`
|
||||
ID uint `json:"id" gorm:"primaryKey"` // 唯一ID
|
||||
Name string `json:"name" gorm:"unique" binding:"required"` // 唯一名称
|
||||
Index int `json:"index"` // 序号 用于排序
|
||||
Type string `json:"type"` // 类型,即driver
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RootFolder string `json:"root_folder"`
|
||||
Status string `json:"status"`
|
||||
Status string `json:"status"` // 状态
|
||||
CronId int
|
||||
DriveId string
|
||||
Limit int `json:"limit"`
|
||||
OrderBy string `json:"order_by"`
|
||||
OrderDirection string `json:"order_direction"`
|
||||
Proxy bool `json:"proxy"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
Search bool `json:"search"`
|
||||
ClientId string `json:"client_id"`
|
||||
@ -31,8 +30,19 @@ type Account struct {
|
||||
RedirectUri string `json:"redirect_uri"`
|
||||
SiteUrl string `json:"site_url"`
|
||||
SiteId string `json:"site_id"`
|
||||
OnedriveType string `json:"onedrive_type"`
|
||||
WebdavProxy bool `json:"webdav_proxy"`
|
||||
InternalType string `json:"internal_type"`
|
||||
WebdavProxy bool `json:"webdav_proxy"` // 开启之后只会webdav走中转
|
||||
Proxy bool `json:"proxy"` // 是否中转,开启之后web和webdav都会走中转
|
||||
//AllowProxy bool `json:"allow_proxy"` // 是否允许中转下载
|
||||
DownProxyUrl string `json:"down_proxy_url"` // 用于中转下载服务的URL 两处 1. path请求中返回的链接 2. down下载时进行302
|
||||
APIProxyUrl string `json:"api_proxy_url"` // 用于中转api的地址
|
||||
// for s3
|
||||
Bucket string `json:"bucket"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Region string `json:"region"`
|
||||
AccessKey string `json:"access_key"`
|
||||
AccessSecret string `json:"access_secret"`
|
||||
CustomHost string `json:"custom_host"`
|
||||
}
|
||||
|
||||
var accountsMap = map[string]Account{}
|
||||
@ -110,6 +120,7 @@ func GetAccountFiles() ([]File, error) {
|
||||
files = append(files, File{
|
||||
Name: v.Name,
|
||||
Size: 0,
|
||||
Driver: v.Type,
|
||||
Type: conf.FOLDER,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
})
|
||||
|
35
model/file_stream.go
Normal file
35
model/file_stream.go
Normal file
@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
import "io"
|
||||
|
||||
type FileStream struct {
|
||||
File io.ReadCloser
|
||||
Size uint64
|
||||
ParentPath string
|
||||
Name string
|
||||
MIMEType string
|
||||
}
|
||||
|
||||
func (file FileStream) Read(p []byte) (n int, err error) {
|
||||
return file.File.Read(p)
|
||||
}
|
||||
|
||||
func (file FileStream) GetMIMEType() string {
|
||||
return file.MIMEType
|
||||
}
|
||||
|
||||
func (file FileStream) GetSize() uint64 {
|
||||
return file.Size
|
||||
}
|
||||
|
||||
func (file FileStream) Close() error {
|
||||
return file.File.Close()
|
||||
}
|
||||
|
||||
func (file FileStream) GetFileName() string {
|
||||
return file.Name
|
||||
}
|
||||
|
||||
func (file FileStream) GetParentPath() string {
|
||||
return file.ParentPath
|
||||
}
|
@ -10,6 +10,7 @@ type Meta struct {
|
||||
Path string `json:"path" gorm:"unique" binding:"required"`
|
||||
Password string `json:"password"`
|
||||
Hide string `json:"hide"`
|
||||
Upload bool `json:"upload"`
|
||||
}
|
||||
|
||||
func GetMetaByPath(path string) (*Meta, error) {
|
||||
|
@ -1,7 +1,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -11,13 +13,31 @@ const (
|
||||
CONST
|
||||
)
|
||||
|
||||
const (
|
||||
FRONT = iota
|
||||
BACK
|
||||
OTHER
|
||||
)
|
||||
|
||||
type SettingItem struct {
|
||||
Key string `json:"key" gorm:"primaryKey" binding:"required"`
|
||||
Value string `json:"value"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
Group int `json:"group"`
|
||||
Access int `json:"access"`
|
||||
Values string `json:"values"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
var Version = SettingItem{
|
||||
Key: "version",
|
||||
Value: conf.GitTag,
|
||||
Description: "version",
|
||||
Type: "string",
|
||||
Access: CONST,
|
||||
Version: conf.GitTag,
|
||||
Group: OTHER,
|
||||
}
|
||||
|
||||
func SaveSettings(items []SettingItem) error {
|
||||
@ -28,20 +48,36 @@ func SaveSetting(item SettingItem) error {
|
||||
return conf.DB.Save(item).Error
|
||||
}
|
||||
|
||||
func GetSettingsPublic() (*[]SettingItem, error) {
|
||||
func GetSettingsPublic() ([]SettingItem, error) {
|
||||
var items []SettingItem
|
||||
if err := conf.DB.Where("`group` <> ?", 1).Find(&items).Error; err != nil {
|
||||
if err := conf.DB.Where("`access` <> ?", 1).Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &items, nil
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func GetSettings() (*[]SettingItem, error) {
|
||||
func GetSettingsByGroup(group int) ([]SettingItem, error) {
|
||||
var items []SettingItem
|
||||
if err := conf.DB.Where("`group` = ?", group).Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append([]SettingItem{Version}, items...)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func GetSettings() ([]SettingItem, error) {
|
||||
var items []SettingItem
|
||||
if err := conf.DB.Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &items, nil
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func DeleteSetting(key string) error {
|
||||
setting := SettingItem{
|
||||
Key: key,
|
||||
}
|
||||
return conf.DB.Delete(&setting).Error
|
||||
}
|
||||
|
||||
func GetSettingByKey(key string) (*SettingItem, error) {
|
||||
@ -61,7 +97,7 @@ func LoadSettings() {
|
||||
if err == nil {
|
||||
conf.CheckParent = checkParent.Value == "true"
|
||||
}
|
||||
checkDown,err := GetSettingByKey("check down link")
|
||||
checkDown, err := GetSettingByKey("check down link")
|
||||
if err == nil {
|
||||
conf.CheckDown = checkDown.Value == "true"
|
||||
}
|
||||
@ -72,18 +108,20 @@ func LoadSettings() {
|
||||
}
|
||||
title, err := GetSettingByKey("title")
|
||||
if err == nil {
|
||||
//conf.CustomizeStyle = customizeStyle.Value
|
||||
conf.IndexHtml = strings.Replace(conf.IndexHtml, "Loading...", title.Value, 1)
|
||||
}
|
||||
customizeStyle, err := GetSettingByKey("customize style")
|
||||
customizeHead, err := GetSettingByKey("customize head")
|
||||
if err == nil {
|
||||
//conf.CustomizeStyle = customizeStyle.Value
|
||||
conf.IndexHtml = strings.Replace(conf.IndexHtml, "/* customize-style */", customizeStyle.Value, 1)
|
||||
conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize head -->", customizeHead.Value, 1)
|
||||
}
|
||||
customizeScript, err := GetSettingByKey("customize script")
|
||||
customizeBody, err := GetSettingByKey("customize body")
|
||||
if err == nil {
|
||||
//conf.CustomizeStyle = customizeScript.Value
|
||||
conf.IndexHtml = strings.Replace(conf.IndexHtml, "// customize-js", customizeScript.Value, 1)
|
||||
conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize body -->", customizeBody.Value, 1)
|
||||
}
|
||||
|
||||
adminPassword, err := GetSettingByKey("password")
|
||||
if err == nil {
|
||||
conf.Token = utils.GetMD5Encode(fmt.Sprintf("https://github.com/Xhofe/alist-%s", adminPassword.Value))
|
||||
}
|
||||
|
||||
davUsername, err := GetSettingByKey("WebDAV username")
|
||||
@ -94,4 +132,12 @@ func LoadSettings() {
|
||||
if err == nil {
|
||||
conf.DavPassword = davPassword.Value
|
||||
}
|
||||
visitorDavUsername, err := GetSettingByKey("Visitor WebDAV username")
|
||||
if err == nil {
|
||||
conf.VisitorDavUsername = visitorDavUsername.Value
|
||||
}
|
||||
visitorDavPassword, err := GetSettingByKey("Visitor WebDAV password")
|
||||
if err == nil {
|
||||
conf.VisitorDavPassword = visitorDavPassword.Value
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +1,17 @@
|
||||
package server
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Auth(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
password, err := model.GetSettingByKey("password")
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
ErrorResp(c, fmt.Errorf("password not set"), 400)
|
||||
return
|
||||
}
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if token != utils.GetMD5Encode(password.Value) {
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
SuccessResp(c)
|
||||
}
|
||||
|
||||
func CheckAccount(c *gin.Context) {
|
||||
if model.AccountsCount() == 0 {
|
||||
ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func CheckParent(path string, password string) bool {
|
||||
meta, err := model.GetMetaByPath(path)
|
||||
if err == nil {
|
||||
@ -55,7 +27,7 @@ func CheckParent(path string, password string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func CheckDownLink(path string, passwordMd5 string) bool {
|
||||
func CheckDownLink(path string, passwordMd5 string, name string) bool {
|
||||
if !conf.CheckDown {
|
||||
return true
|
||||
}
|
||||
@ -63,7 +35,7 @@ func CheckDownLink(path string, passwordMd5 string) bool {
|
||||
log.Debugf("check down path: %s", path)
|
||||
if err == nil {
|
||||
log.Debugf("check down link: %s,%s", meta.Password, passwordMd5)
|
||||
if meta.Password != "" && utils.Get16MD5Encode(meta.Password) != passwordMd5 {
|
||||
if meta.Password != "" && utils.SignWithPassword(name, meta.Password) != passwordMd5 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@ -74,6 +46,6 @@ func CheckDownLink(path string, passwordMd5 string) bool {
|
||||
if path == "/" {
|
||||
return true
|
||||
}
|
||||
return CheckDownLink(utils.Dir(path), passwordMd5)
|
||||
return CheckDownLink(utils.Dir(path), passwordMd5, name)
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package server
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -15,7 +16,12 @@ type Resp struct {
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) {
|
||||
type PathReq struct {
|
||||
Path string `json:"path"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
||||
var path, name string
|
||||
switch model.AccountsCount() {
|
||||
case 0:
|
||||
@ -24,6 +30,9 @@ func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) {
|
||||
path = rawPath
|
||||
break
|
||||
default:
|
||||
if path == "/" {
|
||||
return nil, "", nil, errors.New("can't operate root of multiple accounts")
|
||||
}
|
||||
paths := strings.Split(rawPath, "/")
|
||||
path = "/" + strings.Join(paths[2:], "/")
|
||||
name = paths[1]
|
||||
@ -32,7 +41,7 @@ func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) {
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] account", name)
|
||||
}
|
||||
driver, ok := drivers.GetDriver(account.Type)
|
||||
driver, ok := base.GetDriver(account.Type)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
package server
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strconv"
|
||||
@ -13,52 +14,52 @@ import (
|
||||
func GetAccounts(c *gin.Context) {
|
||||
accounts, err := model.GetAccounts()
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c, accounts)
|
||||
common.SuccessResp(c, accounts)
|
||||
}
|
||||
|
||||
func CreateAccount(c *gin.Context) {
|
||||
var req model.Account
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
driver, ok := drivers.GetDriver(req.Type)
|
||||
driver, ok := base.GetDriver(req.Type)
|
||||
if !ok {
|
||||
ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
common.ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
req.UpdatedAt = &now
|
||||
if err := model.CreateAccount(&req); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
log.Debugf("new account: %+v", req)
|
||||
err = driver.Save(&req, nil)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveAccount(c *gin.Context) {
|
||||
var req model.Account
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
driver, ok := drivers.GetDriver(req.Type)
|
||||
driver, ok := base.GetDriver(req.Type)
|
||||
if !ok {
|
||||
ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
common.ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400)
|
||||
return
|
||||
}
|
||||
old, err := model.GetAccountById(req.ID)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
@ -67,15 +68,15 @@ func SaveAccount(c *gin.Context) {
|
||||
model.DeleteAccountFromMap(old.Name)
|
||||
}
|
||||
if err := model.SaveAccount(&req); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
log.Debugf("save account: %+v", req)
|
||||
err = driver.Save(&req, old)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,12 +84,12 @@ func DeleteAccount(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := model.DeleteAccount(uint(id)); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
package server
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ClearCache(c *gin.Context) {
|
||||
err := conf.Cache.Clear(conf.Ctx)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
167
server/controllers/down.go
Normal file
167
server/controllers/down.go
Normal file
@ -0,0 +1,167 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
rawPath := c.Param("path")
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("down: %s", rawPath)
|
||||
account, path, driver, err := common.ParsePath(rawPath)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if driver.Config().OnlyProxy || account.Proxy {
|
||||
Proxy(c)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
c.Redirect(302, link.Url)
|
||||
return
|
||||
}
|
||||
|
||||
func Proxy(c *gin.Context) {
|
||||
rawPath := c.Param("path")
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("proxy: %s", rawPath)
|
||||
account, path, driver, err := common.ParsePath(rawPath)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
// 只有以下几种情况允许中转:
|
||||
// 1. 账号开启中转
|
||||
// 2. driver只能中转
|
||||
// 3. 是文本类型文件
|
||||
// 4. 开启webdav中转(需要验证sign)
|
||||
if !account.Proxy && !driver.Config().OnlyProxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
||||
// 只开启了webdav中转,验证sign
|
||||
ok := false
|
||||
if account.WebdavProxy {
|
||||
_, ok = c.Get("sign")
|
||||
}
|
||||
if !ok {
|
||||
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 中转时有中转机器使用中转机器,若携带标志位则表明不能再走中转机器了
|
||||
if account.DownProxyUrl != "" && c.Param("d") != "1" {
|
||||
name := utils.Base(rawPath)
|
||||
link := fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
|
||||
c.Redirect(302, link)
|
||||
return
|
||||
}
|
||||
// 对于中转,不需要重设IP
|
||||
link, err := driver.Link(base.Args{Path: path}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
// 本机读取数据
|
||||
if link.Data != nil {
|
||||
//c.Data(http.StatusOK, "application/octet-stream", link.Data)
|
||||
defer func() {
|
||||
_ = link.Data.Close()
|
||||
}()
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("content", "application/octet-stream")
|
||||
_, err = io.Copy(c.Writer, link.Data)
|
||||
if err != nil {
|
||||
_, _ = c.Writer.WriteString(err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
// 本机文件直接返回文件
|
||||
if account.Type == "Native" {
|
||||
// 对于名称为index.html的文件需要特殊处理
|
||||
if utils.Base(rawPath) == "index.html" {
|
||||
file, err := os.Open(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
fileStat, err := os.Stat(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
http.ServeContent(c.Writer, c.Request, utils.Base(rawPath), fileStat.ModTime(), file)
|
||||
return
|
||||
}
|
||||
c.File(link.Url)
|
||||
return
|
||||
} else {
|
||||
if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT {
|
||||
Text(c, link)
|
||||
return
|
||||
}
|
||||
driver.Proxy(c, account)
|
||||
r := c.Request
|
||||
w := c.Writer
|
||||
target, err := url.Parse(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
protocol := "http://"
|
||||
if strings.HasPrefix(link.Url, "https://") {
|
||||
protocol = "https://"
|
||||
}
|
||||
targetHost, err := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host))
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetHost)
|
||||
r.URL = target
|
||||
r.Host = target.Host
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
var client *resty.Client
|
||||
|
||||
func init() {
|
||||
client = resty.New()
|
||||
client.SetRetryCount(3)
|
||||
}
|
||||
|
||||
func Text(c *gin.Context, link *base.Link) {
|
||||
res, err := client.R().Get(link.Url)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
text := res.String()
|
||||
t := utils.GetStrCoding(res.Body())
|
||||
log.Debugf("text type: %s", t)
|
||||
if t != utils.UTF8 {
|
||||
body, err := utils.GbkToUtf8(res.Body())
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
text = string(body)
|
||||
}
|
||||
c.String(200, text)
|
||||
}
|
11
server/controllers/driver.go
Normal file
11
server/controllers/driver.go
Normal file
@ -0,0 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetDrivers(c *gin.Context) {
|
||||
common.SuccessResp(c, base.GetDrivers())
|
||||
}
|
57
server/controllers/file.go
Normal file
57
server/controllers/file.go
Normal file
@ -0,0 +1,57 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func UploadFile(c *gin.Context) {
|
||||
path := c.PostForm("path")
|
||||
path = utils.ParsePath(path)
|
||||
token := c.GetHeader("Authorization")
|
||||
if token != conf.Token {
|
||||
password := c.PostForm("password")
|
||||
meta, _ := model.GetMetaByPath(path)
|
||||
if meta == nil || !meta.Upload {
|
||||
common.ErrorResp(c, errors.New("not allow upload"), 403)
|
||||
return
|
||||
}
|
||||
if meta.Password != "" && meta.Password != password {
|
||||
common.ErrorResp(c, errors.New("wrong password"), 403)
|
||||
return
|
||||
}
|
||||
}
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
}
|
||||
open, err := file.Open()
|
||||
defer func() {
|
||||
_ = open.Close()
|
||||
}()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
account, path_, driver, err := common.ParsePath(path)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
fileStream := model.FileStream{
|
||||
File: open,
|
||||
Size: uint64(file.Size),
|
||||
ParentPath: path_,
|
||||
Name: file.Filename,
|
||||
MIMEType: c.GetHeader("Content-Type"),
|
||||
}
|
||||
err = driver.Upload(&fileStream, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c)
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
package server
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
@ -10,37 +11,37 @@ import (
|
||||
func GetMetas(c *gin.Context) {
|
||||
metas,err := model.GetMetas()
|
||||
if err != nil {
|
||||
ErrorResp(c,err,500)
|
||||
common.ErrorResp(c,err,500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c, metas)
|
||||
common.SuccessResp(c, metas)
|
||||
}
|
||||
|
||||
func CreateMeta(c *gin.Context) {
|
||||
var req model.Meta
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
if err := model.CreateMeta(req); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveMeta(c *gin.Context) {
|
||||
var req model.Meta
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
if err := model.SaveMeta(req); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,13 +49,13 @@ func DeleteMeta(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
//path = utils.ParsePath(path)
|
||||
if err := model.DeleteMeta(uint(id)); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
SuccessResp(c)
|
||||
common.SuccessResp(c)
|
||||
}
|
174
server/controllers/path.go
Normal file
174
server/controllers/path.go
Normal file
@ -0,0 +1,174 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Hide(meta *model.Meta, files []model.File, path string) []model.File {
|
||||
//meta, _ := model.GetMetaByPath(path)
|
||||
if meta != nil && meta.Hide != "" {
|
||||
tmpFiles := make([]model.File, 0)
|
||||
hideFiles := strings.Split(meta.Hide, ",")
|
||||
for _, item := range files {
|
||||
if !utils.IsContain(hideFiles, item.Name) {
|
||||
tmpFiles = append(tmpFiles, item)
|
||||
}
|
||||
}
|
||||
files = tmpFiles
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
Driver string `json:"driver"`
|
||||
Upload bool `json:"upload"`
|
||||
}
|
||||
|
||||
type PathResp struct {
|
||||
Type string `json:"type"`
|
||||
Meta Meta `json:"meta"`
|
||||
Files []model.File `json:"files"`
|
||||
}
|
||||
|
||||
func Path(c *gin.Context) {
|
||||
reqV, _ := c.Get("req")
|
||||
req := reqV.(common.PathReq)
|
||||
meta, _ := model.GetMetaByPath(req.Path)
|
||||
upload := false
|
||||
if meta != nil && meta.Upload {
|
||||
upload = true
|
||||
}
|
||||
if model.AccountsCount() > 1 && req.Path == "/" {
|
||||
files, err := model.GetAccountFiles()
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
files = Hide(meta, files, req.Path)
|
||||
c.JSON(200, common.Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: PathResp{
|
||||
Type: "folder",
|
||||
Meta: Meta{
|
||||
Driver: "root",
|
||||
},
|
||||
Files: files,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
account, path, driver, err := common.ParsePath(req.Path)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
file, files, err := driver.Path(path, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if file != nil {
|
||||
// 对于中转文件或只能中转,将链接修改为中转链接
|
||||
if driver.Config().OnlyProxy || account.Proxy {
|
||||
if account.DownProxyUrl != "" {
|
||||
file.Url = fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
|
||||
} else {
|
||||
file.Url = fmt.Sprintf("//%s/p%s?sign=%s", c.Request.Host, req.Path, utils.SignWithToken(file.Name, conf.Token))
|
||||
}
|
||||
} else if !driver.Config().NoNeedSetLink {
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
file.Url = link.Url
|
||||
}
|
||||
c.JSON(200, common.Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: PathResp{
|
||||
Type: "file",
|
||||
Meta: Meta{
|
||||
Driver: driver.Config().Name,
|
||||
},
|
||||
Files: []model.File{*file},
|
||||
},
|
||||
})
|
||||
} else {
|
||||
files = Hide(meta, files, req.Path)
|
||||
if driver.Config().LocalSort {
|
||||
model.SortFiles(files, account)
|
||||
}
|
||||
c.JSON(200, common.Resp{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: PathResp{
|
||||
Type: "folder",
|
||||
Meta: Meta{
|
||||
Driver: driver.Config().Name,
|
||||
Upload: upload,
|
||||
},
|
||||
Files: files,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Link 返回真实的链接,且携带头,只提供给中转程序使用
|
||||
func Link(c *gin.Context) {
|
||||
var req common.PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("link: %s", rawPath)
|
||||
account, path, driver, err := common.ParsePath(rawPath)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if driver.Config().OnlyLocal {
|
||||
common.SuccessResp(c, base.Link{
|
||||
Url: fmt.Sprintf("//%s/p%s?d=1&sign=%s", c.Request.Host, req.Path, utils.SignWithToken(utils.Base(rawPath), conf.Token)),
|
||||
})
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c, link)
|
||||
return
|
||||
}
|
||||
|
||||
func Preview(c *gin.Context) {
|
||||
reqV, _ := c.Get("req")
|
||||
req := reqV.(common.PathReq)
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("preview: %s", rawPath)
|
||||
account, path, driver, err := common.ParsePath(rawPath)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
data, err := driver.Preview(path, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
common.SuccessResp(c, data)
|
||||
}
|
||||
}
|
71
server/controllers/setting.go
Normal file
71
server/controllers/setting.go
Normal file
@ -0,0 +1,71 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func SaveSettings(c *gin.Context) {
|
||||
var req []model.SettingItem
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := model.SaveSettings(req); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
model.LoadSettings()
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSettings(c *gin.Context) {
|
||||
groupStr := c.Query("group")
|
||||
var settings []model.SettingItem
|
||||
var err error
|
||||
if groupStr == "" {
|
||||
settings, err = model.GetSettings()
|
||||
} else {
|
||||
group, err := strconv.Atoi(groupStr)
|
||||
if err == nil {
|
||||
settings, err = model.GetSettingsByGroup(group)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c, settings)
|
||||
}
|
||||
|
||||
func GetSettingsPublic(c *gin.Context) {
|
||||
settings, err := model.GetSettingsPublic()
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
settings = append(settings, []model.SettingItem{{
|
||||
Key: "no cors",
|
||||
Value: drivers.NoCors,
|
||||
Description: "",
|
||||
Type: "string",
|
||||
}, {
|
||||
Key: "no upload",
|
||||
Value: drivers.NoUpload,
|
||||
Description: "",
|
||||
Type: "string",
|
||||
}}...)
|
||||
common.SuccessResp(c, settings)
|
||||
}
|
||||
|
||||
func DeleteSetting(c *gin.Context) {
|
||||
key := c.Query("key")
|
||||
if err := model.DeleteSetting(key); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c)
|
||||
}
|
124
server/down.go
124
server/down.go
@ -1,124 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
rawPath := c.Param("path")
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("down: %s", rawPath)
|
||||
pw := c.Query("pw")
|
||||
if !CheckDownLink(utils.Dir(rawPath), pw) {
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "GoogleDrive" {
|
||||
Proxy(c)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
c.File(link)
|
||||
return
|
||||
} else {
|
||||
c.Redirect(302, link)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Proxy(c *gin.Context) {
|
||||
rawPath := c.Param("path")
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("proxy: %s", rawPath)
|
||||
pw := c.Query("pw")
|
||||
if !CheckDownLink(utils.Dir(rawPath), pw) {
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
||||
ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
c.File(link)
|
||||
return
|
||||
} else {
|
||||
if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT {
|
||||
Text(c, link)
|
||||
return
|
||||
}
|
||||
driver.Proxy(c, account)
|
||||
r := c.Request
|
||||
w := c.Writer
|
||||
target, err := url.Parse(link)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
protocol := "http://"
|
||||
if strings.HasPrefix(link, "https://") {
|
||||
protocol = "https://"
|
||||
}
|
||||
targetHost, err := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host))
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetHost)
|
||||
r.URL = target
|
||||
r.Host = target.Host
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
var client *resty.Client
|
||||
|
||||
func init() {
|
||||
client = resty.New()
|
||||
client.SetRetryCount(3)
|
||||
}
|
||||
|
||||
func Text(c *gin.Context, link string) {
|
||||
res, err := client.R().Get(link)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
text := res.String()
|
||||
t := utils.GetStrCoding(res.Body())
|
||||
log.Debugf("text type: %s", t)
|
||||
if t != utils.UTF8 {
|
||||
body, err := utils.GbkToUtf8(res.Body())
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
text = string(body)
|
||||
}
|
||||
c.String(200, text)
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetDrivers(c *gin.Context) {
|
||||
SuccessResp(c, drivers.GetDrivers())
|
||||
}
|
16
server/middlewares/account.go
Normal file
16
server/middlewares/account.go
Normal file
@ -0,0 +1,16 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CheckAccount(c *gin.Context) {
|
||||
if model.AccountsCount() == 0 {
|
||||
common.ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
27
server/middlewares/auth.go
Normal file
27
server/middlewares/auth.go
Normal file
@ -0,0 +1,27 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Auth(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
//password, err := model.GetSettingByKey("password")
|
||||
//if err != nil {
|
||||
// if err == gorm.ErrRecordNotFound {
|
||||
// common.ErrorResp(c, fmt.Errorf("password not set"), 400)
|
||||
// return
|
||||
// }
|
||||
// common.ErrorResp(c, err, 500)
|
||||
// return
|
||||
//}
|
||||
//if token != utils.GetMD5Encode(password.Value) {
|
||||
if token != conf.Token {
|
||||
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
28
server/middlewares/down.go
Normal file
28
server/middlewares/down.go
Normal file
@ -0,0 +1,28 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func DownCheck(c *gin.Context) {
|
||||
sign := c.Query("sign")
|
||||
rawPath := c.Param("path")
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
name := utils.Base(rawPath)
|
||||
if sign == utils.SignWithToken(name, conf.Token) {
|
||||
c.Set("sign", true)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
pw := c.Query("pw")
|
||||
if !common.CheckDownLink(utils.Dir(rawPath), pw, utils.Base(rawPath)) {
|
||||
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
40
server/middlewares/path.go
Normal file
40
server/middlewares/path.go
Normal file
@ -0,0 +1,40 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PathCheck(c *gin.Context) {
|
||||
var req common.PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
c.Set("req",req)
|
||||
token := c.GetHeader("Authorization")
|
||||
if token == conf.Token {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
meta, err := model.GetMetaByPath(req.Path)
|
||||
if err == nil {
|
||||
if meta.Password != "" && meta.Password != req.Password {
|
||||
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else if conf.CheckParent {
|
||||
if !common.CheckParent(utils.Dir(req.Path), req.Password) {
|
||||
common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
}
|
142
server/path.go
142
server/path.go
@ -1,142 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PathReq struct {
|
||||
Path string `json:"Path"`
|
||||
Password string `json:"Password"`
|
||||
}
|
||||
|
||||
func Path(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Path = utils.ParsePath(req.Path)
|
||||
log.Debugf("path: %s", req.Path)
|
||||
meta, err := model.GetMetaByPath(req.Path)
|
||||
if err == nil {
|
||||
if meta.Password != "" && meta.Password != req.Password {
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
// TODO hide or ignore?
|
||||
} else if conf.CheckParent {
|
||||
if !CheckParent(utils.Dir(req.Path), req.Password) {
|
||||
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
||||
return
|
||||
}
|
||||
}
|
||||
if model.AccountsCount() > 1 && req.Path == "/" {
|
||||
files, err := model.GetAccountFiles()
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "folder",
|
||||
Data: files,
|
||||
})
|
||||
return
|
||||
}
|
||||
account, path, driver, err := ParsePath(req.Path)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
file, files, err := driver.Path(path, account)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if file != nil {
|
||||
if account.Type == "Native" {
|
||||
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
|
||||
}
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "file",
|
||||
Data: []*model.File{file},
|
||||
})
|
||||
} else {
|
||||
if meta != nil && meta.Hide != "" {
|
||||
tmpFiles := make([]model.File, 0)
|
||||
hideFiles := strings.Split(meta.Hide, ",")
|
||||
for _, item := range files {
|
||||
if !utils.IsContain(hideFiles, item.Name) {
|
||||
tmpFiles = append(tmpFiles, item)
|
||||
}
|
||||
}
|
||||
files = tmpFiles
|
||||
}
|
||||
c.JSON(200, Resp{
|
||||
Code: 200,
|
||||
Message: "folder",
|
||||
Data: files,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Link(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("link: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
if account.Type == "Native" {
|
||||
SuccessResp(c, gin.H{
|
||||
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
|
||||
})
|
||||
return
|
||||
} else {
|
||||
SuccessResp(c, gin.H{
|
||||
"url": link,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Preview(c *gin.Context) {
|
||||
var req PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
rawPath := req.Path
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("preview: %s", rawPath)
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
data, err := driver.Preview(path, account)
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
SuccessResp(c, data)
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/server/common"
|
||||
"github.com/Xhofe/alist/server/controllers"
|
||||
"github.com/Xhofe/alist/server/middlewares"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -9,38 +12,46 @@ func InitApiRouter(r *gin.Engine) {
|
||||
|
||||
// TODO from settings
|
||||
Cors(r)
|
||||
r.GET("/d/*path", Down)
|
||||
r.GET("/p/*path", Proxy)
|
||||
r.GET("/d/*path", middlewares.DownCheck, controllers.Down)
|
||||
r.GET("/p/*path", middlewares.DownCheck, controllers.Proxy)
|
||||
|
||||
api := r.Group("/api")
|
||||
public := api.Group("/public")
|
||||
{
|
||||
public.POST("/path", CheckAccount, Path)
|
||||
public.POST("/preview", CheckAccount, Preview)
|
||||
public.GET("/settings", GetSettingsPublic)
|
||||
public.POST("/link", CheckAccount, Link)
|
||||
path := public.Group("", middlewares.PathCheck, middlewares.CheckAccount)
|
||||
path.POST("/path", controllers.Path)
|
||||
path.POST("/preview", controllers.Preview)
|
||||
|
||||
//path.POST("/link",middlewares.Auth, controllers.Link)
|
||||
public.POST("/upload", controllers.UploadFile)
|
||||
|
||||
public.GET("/settings", controllers.GetSettingsPublic)
|
||||
}
|
||||
|
||||
admin := api.Group("/admin")
|
||||
{
|
||||
admin.Use(Auth)
|
||||
admin.GET("/login", Login)
|
||||
admin.GET("/settings", GetSettings)
|
||||
admin.POST("/settings", SaveSettings)
|
||||
admin.POST("/account/create", CreateAccount)
|
||||
admin.POST("/account/save", SaveAccount)
|
||||
admin.GET("/accounts", GetAccounts)
|
||||
admin.DELETE("/account", DeleteAccount)
|
||||
admin.GET("/drivers", GetDrivers)
|
||||
admin.GET("/clear_cache", ClearCache)
|
||||
admin.Use(middlewares.Auth)
|
||||
admin.GET("/login", common.Login)
|
||||
admin.GET("/settings", controllers.GetSettings)
|
||||
admin.POST("/settings", controllers.SaveSettings)
|
||||
admin.DELETE("/setting", controllers.DeleteSetting)
|
||||
|
||||
admin.GET("/metas", GetMetas)
|
||||
admin.POST("/meta/create", CreateMeta)
|
||||
admin.POST("/meta/save", SaveMeta)
|
||||
admin.DELETE("/meta", DeleteMeta)
|
||||
admin.POST("/account/create", controllers.CreateAccount)
|
||||
admin.POST("/account/save", controllers.SaveAccount)
|
||||
admin.GET("/accounts", controllers.GetAccounts)
|
||||
admin.DELETE("/account", controllers.DeleteAccount)
|
||||
admin.GET("/drivers", controllers.GetDrivers)
|
||||
admin.GET("/clear_cache", controllers.ClearCache)
|
||||
|
||||
admin.GET("/metas", controllers.GetMetas)
|
||||
admin.POST("/meta/create", controllers.CreateMeta)
|
||||
admin.POST("/meta/save", controllers.SaveMeta)
|
||||
admin.DELETE("/meta", controllers.DeleteMeta)
|
||||
|
||||
admin.POST("/link", controllers.Link)
|
||||
}
|
||||
Static(r)
|
||||
WebDav(r)
|
||||
Static(r)
|
||||
}
|
||||
|
||||
func Cors(r *gin.Engine) {
|
||||
|
@ -1,38 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SaveSettings(c *gin.Context) {
|
||||
var req []model.SettingItem
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := model.SaveSettings(req); err != nil {
|
||||
ErrorResp(c, err, 500)
|
||||
} else {
|
||||
model.LoadSettings()
|
||||
SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSettings(c *gin.Context) {
|
||||
settings, err := model.GetSettings()
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
SuccessResp(c, settings)
|
||||
}
|
||||
|
||||
func GetSettingsPublic(c *gin.Context) {
|
||||
settings, err := model.GetSettingsPublic()
|
||||
if err != nil {
|
||||
ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
SuccessResp(c, settings)
|
||||
}
|
@ -10,11 +10,16 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
func init() {
|
||||
index, err := public.Public.Open("index.html")
|
||||
func InitIndex() {
|
||||
var index fs.File
|
||||
var err error
|
||||
if conf.Conf.Local {
|
||||
index, err = public.Public.Open("local.html")
|
||||
} else {
|
||||
index, err = public.Public.Open("index.html")
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf(err.Error())
|
||||
//log.Fatalf(err.Error())
|
||||
return
|
||||
}
|
||||
data, _ := ioutil.ReadAll(index)
|
||||
@ -22,11 +27,17 @@ func init() {
|
||||
}
|
||||
|
||||
func Static(r *gin.Engine) {
|
||||
//InitIndex()
|
||||
assets, err := fs.Sub(public.Public, "assets")
|
||||
if err != nil {
|
||||
log.Fatalf("can't find assets folder")
|
||||
}
|
||||
pub, err := fs.Sub(public.Public, "public")
|
||||
if err != nil {
|
||||
log.Fatalf("can't find public folder")
|
||||
}
|
||||
r.StaticFS("/assets/", http.FS(assets))
|
||||
r.StaticFS("/public/", http.FS(pub))
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
c.Status(200)
|
||||
c.Header("Content-Type", "text/html")
|
||||
|
@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/webdav"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
@ -33,7 +34,7 @@ func WebDav(r *gin.Engine) {
|
||||
|
||||
func ServeWebDAV(c *gin.Context) {
|
||||
fs := webdav.FileSystem{}
|
||||
handler.ServeHTTP(c.Writer,c.Request,&fs)
|
||||
handler.ServeHTTP(c.Writer, c.Request, &fs)
|
||||
}
|
||||
|
||||
func WebDAVAuth(c *gin.Context) {
|
||||
@ -48,13 +49,16 @@ func WebDAVAuth(c *gin.Context) {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if conf.DavUsername != "" && conf.DavUsername != username {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
c.Abort()
|
||||
if conf.DavUsername == username && conf.DavPassword == password {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if conf.DavPassword != "" && conf.DavPassword != password {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
c.Abort()
|
||||
if (conf.VisitorDavUsername == username && conf.VisitorDavPassword == password) || (conf.VisitorDavUsername == "" && conf.VisitorDavPassword == "") {
|
||||
if !utils.IsContain([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
c.Status(http.StatusUnauthorized)
|
||||
c.Abort()
|
||||
}
|
||||
|
@ -8,10 +8,11 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@ -21,28 +22,28 @@ import (
|
||||
|
||||
type FileSystem struct{}
|
||||
|
||||
func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) {
|
||||
var path, name string
|
||||
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
||||
var internalPath, name string
|
||||
switch model.AccountsCount() {
|
||||
case 0:
|
||||
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
|
||||
case 1:
|
||||
path = rawPath
|
||||
internalPath = rawPath
|
||||
break
|
||||
default:
|
||||
paths := strings.Split(rawPath, "/")
|
||||
path = "/" + strings.Join(paths[2:], "/")
|
||||
internalPath = "/" + strings.Join(paths[2:], "/")
|
||||
name = paths[1]
|
||||
}
|
||||
account, ok := model.GetAccount(name)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] account", name)
|
||||
}
|
||||
driver, ok := drivers.GetDriver(account.Type)
|
||||
driver, ok := base.GetDriver(account.Type)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
|
||||
}
|
||||
return &account, path, driver, nil
|
||||
return &account, internalPath, driver, nil
|
||||
}
|
||||
|
||||
func (fs *FileSystem) File(rawPath string) (*model.File, error) {
|
||||
@ -80,25 +81,44 @@ func (fs *FileSystem) Files(rawPath string) ([]model.File, error) {
|
||||
return driver.Files(path_, account)
|
||||
}
|
||||
|
||||
func GetPW(path string) string {
|
||||
if !conf.CheckDown {
|
||||
return ""
|
||||
//func GetPW(path string, name string) string {
|
||||
// if !conf.CheckDown {
|
||||
// return ""
|
||||
// }
|
||||
// meta, err := model.GetMetaByPath(path)
|
||||
// if err == nil {
|
||||
// if meta.Password != "" {
|
||||
// return utils.SignWithPassword(name, meta.Password)
|
||||
// }
|
||||
// return ""
|
||||
// } else {
|
||||
// if !conf.CheckParent {
|
||||
// return ""
|
||||
// }
|
||||
// if path == "/" {
|
||||
// return ""
|
||||
// }
|
||||
// return GetPW(utils.Dir(path), name)
|
||||
// }
|
||||
//}
|
||||
|
||||
func ClientIP(r *http.Request) string {
|
||||
xForwardedFor := r.Header.Get("X-Forwarded-For")
|
||||
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
meta, err := model.GetMetaByPath(path)
|
||||
if err == nil {
|
||||
if meta.Password != "" {
|
||||
utils.Get16MD5Encode(meta.Password)
|
||||
}
|
||||
return ""
|
||||
} else {
|
||||
if !conf.CheckParent {
|
||||
return ""
|
||||
}
|
||||
if path == "/" {
|
||||
return ""
|
||||
}
|
||||
return GetPW(utils.Dir(path))
|
||||
|
||||
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
|
||||
return ip
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
|
||||
@ -119,18 +139,74 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
|
||||
if driver.Config().OnlyProxy || account.WebdavProxy {
|
||||
link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath)
|
||||
if conf.CheckDown {
|
||||
pw := GetPW(utils.Dir(rawPath))
|
||||
link += "?pw" + pw
|
||||
sign := utils.SignWithToken(utils.Base(rawPath), conf.Token)
|
||||
link += "?sign" + sign
|
||||
}
|
||||
} else {
|
||||
link, err = driver.Link(path_, account)
|
||||
link_, err := driver.Link(base.Args{Path: path_, IP: ClientIP(r)}, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
link = link_.Url
|
||||
}
|
||||
log.Debugf("webdav get link: %s", link)
|
||||
return link, err
|
||||
}
|
||||
|
||||
func (fs *FileSystem) CreateDirectory(ctx context.Context, reqPath string) (interface{}, error) {
|
||||
return nil, nil
|
||||
func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error {
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
if rawPath == "/" {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
if model.AccountsCount() > 1 && len(strings.Split(rawPath, "/")) < 2 {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
account, path_, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("mkdir: %s", path_)
|
||||
return driver.MakeDir(path_, account)
|
||||
}
|
||||
|
||||
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
if model.AccountsCount() > 1 && rawPath == "/" {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
account, path_, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64)
|
||||
fileSize := uint64(r.ContentLength)
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
filePath, fileName := filepath.Split(path_)
|
||||
fileData := model.FileStream{
|
||||
MIMEType: r.Header.Get("Content-Type"),
|
||||
File: r.Body,
|
||||
Size: fileSize,
|
||||
Name: fileName,
|
||||
ParentPath: filePath,
|
||||
}
|
||||
return driver.Upload(&fileData, account)
|
||||
}
|
||||
|
||||
func (fs *FileSystem) Delete(rawPath string) error {
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
if rawPath == "/" {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
if model.AccountsCount() > 1 && len(strings.Split(rawPath, "/")) < 2 {
|
||||
return ErrNotImplemented
|
||||
}
|
||||
account, path_, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return driver.Delete(path_, account)
|
||||
}
|
||||
|
||||
// slashClean is equivalent to but slightly more efficient than
|
||||
@ -145,16 +221,58 @@ func slashClean(name string) string {
|
||||
// moveFiles moves files and/or directories from src to dst.
|
||||
//
|
||||
// See section 9.9.4 for when various HTTP status codes apply.
|
||||
func moveFiles(ctx context.Context, fs *FileSystem, src FileInfo, dst string, overwrite bool) (status int, err error) {
|
||||
|
||||
func moveFiles(ctx context.Context, fs *FileSystem, src string, dst string, overwrite bool) (status int, err error) {
|
||||
src = utils.ParsePath(src)
|
||||
dst = utils.ParsePath(dst)
|
||||
log.Debugf("move %s -> %s", src, dst)
|
||||
if src == dst {
|
||||
return http.StatusMethodNotAllowed, errDestinationEqualsSource
|
||||
}
|
||||
srcAccount, srcPath, driver, err := ParsePath(src)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
dstAccount, dstPath, _, err := ParsePath(dst)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
if srcAccount.Name != dstAccount.Name {
|
||||
return http.StatusMethodNotAllowed, errInvalidDestination
|
||||
}
|
||||
err = driver.Move(srcPath, dstPath, srcAccount)
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
// copyFiles copies files and/or directories from src to dst.
|
||||
//
|
||||
// See section 9.8.5 for when various HTTP status codes apply.
|
||||
func copyFiles(ctx context.Context, fs *FileSystem, src FileInfo, dst string, overwrite bool, depth int, recursion int) (status int, err error) {
|
||||
|
||||
func copyFiles(ctx context.Context, fs *FileSystem, src string, dst string, overwrite bool, depth int, recursion int) (status int, err error) {
|
||||
src = utils.ParsePath(src)
|
||||
dst = utils.ParsePath(dst)
|
||||
log.Debugf("move %s -> %s", src, dst)
|
||||
if src == dst {
|
||||
return http.StatusMethodNotAllowed, errDestinationEqualsSource
|
||||
}
|
||||
srcAccount, srcPath, driver, err := ParsePath(src)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
dstAccount, dstPath, _, err := ParsePath(dst)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
if srcAccount.Name != dstAccount.Name {
|
||||
// TODO 跨账号复制
|
||||
return http.StatusMethodNotAllowed, errInvalidDestination
|
||||
}
|
||||
err = driver.Copy(srcPath, dstPath, srcAccount)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,7 @@ func (h *Handler) stripPrefix(p string) (string, int, error) {
|
||||
func isPathExist(ctx context.Context, fs *FileSystem, path string) (bool, FileInfo) {
|
||||
file, err := fs.File(path)
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
return false, nil
|
||||
}
|
||||
return true, file
|
||||
@ -85,7 +86,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *FileSyst
|
||||
if status != 0 {
|
||||
w.WriteHeader(status)
|
||||
if status != http.StatusNoContent {
|
||||
w.Write([]byte(StatusText(status)))
|
||||
_, _ = w.Write([]byte(StatusText(status)))
|
||||
}
|
||||
}
|
||||
if h.Logger != nil {
|
||||
@ -221,7 +222,10 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
if reqPath == "/" {
|
||||
_, err = w.Write([]byte("Please connect using software that supports WebDAV instead of a browser.\n"))
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
exist, file := isPathExist(ctx, fs, reqPath)
|
||||
if !exist {
|
||||
return http.StatusNotFound, nil
|
||||
@ -253,26 +257,11 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *FileS
|
||||
return status, err
|
||||
}
|
||||
defer release()
|
||||
|
||||
//ctx := r.Context()
|
||||
|
||||
//// 尝试作为文件删除
|
||||
//if ok, file := fs.IsFileExist(reqPath); ok {
|
||||
// if err := fs.Delete(ctx, []uint{}, []uint{file.ID}, false); err != nil {
|
||||
// return http.StatusMethodNotAllowed, err
|
||||
// }
|
||||
// return http.StatusNoContent, nil
|
||||
//}
|
||||
//
|
||||
//// 尝试作为目录删除
|
||||
//if ok, folder := fs.IsPathExist(reqPath); ok {
|
||||
// if err := fs.Delete(ctx, []uint{folder.ID}, []uint{}, false); err != nil {
|
||||
// return http.StatusMethodNotAllowed, err
|
||||
// }
|
||||
// return http.StatusNoContent, nil
|
||||
//}
|
||||
|
||||
return http.StatusNotFound, nil
|
||||
err = fs.Delete(reqPath)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
// OK
|
||||
@ -291,7 +280,13 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSyst
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
etag, err := findETag(ctx, fs, h.LockSystem, reqPath, nil)
|
||||
err = fs.Upload(ctx, r, reqPath)
|
||||
if err != nil {
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
|
||||
_, fi := isPathExist(ctx, fs, reqPath)
|
||||
etag, err := findETag(ctx, fs, h.LockSystem, reqPath, fi)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
@ -321,7 +316,7 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *FileSy
|
||||
// ctx = context.WithValue(ctx, fsctx.IgnoreDirectoryConflictCtx, true)
|
||||
//}
|
||||
}
|
||||
if _, err := fs.CreateDirectory(ctx, reqPath); err != nil {
|
||||
if err := fs.CreateDirectory(ctx, reqPath); err != nil {
|
||||
return http.StatusConflict, err
|
||||
}
|
||||
return http.StatusCreated, nil
|
||||
@ -361,7 +356,7 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *Fil
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
isExist, target := isPathExist(ctx, fs, src)
|
||||
isExist, _ := isPathExist(ctx, fs, src)
|
||||
|
||||
if !isExist {
|
||||
return http.StatusNotFound, nil
|
||||
@ -390,7 +385,7 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *Fil
|
||||
return http.StatusBadRequest, errInvalidDepth
|
||||
}
|
||||
}
|
||||
return copyFiles(ctx, fs, target, dst, r.Header.Get("Overwrite") != "F", depth, 0)
|
||||
return copyFiles(ctx, fs, src, dst, r.Header.Get("Overwrite") != "F", depth, 0)
|
||||
}
|
||||
|
||||
// windows下,某些情况下(网盘根目录下)Office保存文件时附带的锁token只包含源文件,
|
||||
@ -409,7 +404,7 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *Fil
|
||||
return http.StatusBadRequest, errInvalidDepth
|
||||
}
|
||||
}
|
||||
return moveFiles(ctx, fs, target, dst, r.Header.Get("Overwrite") == "T")
|
||||
return moveFiles(ctx, fs, src, dst, r.Header.Get("Overwrite") == "T")
|
||||
}
|
||||
|
||||
// OK
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
@ -34,7 +35,7 @@ func GetFileType(ext string) int {
|
||||
if ext == "" {
|
||||
return conf.UNKNOWN
|
||||
}
|
||||
ext = strings.ToLower(strings.TrimLeft(ext,"."))
|
||||
ext = strings.ToLower(strings.TrimLeft(ext, "."))
|
||||
if IsContain(conf.OfficeTypes, ext) {
|
||||
return conf.OFFICE
|
||||
}
|
||||
@ -105,4 +106,20 @@ func Dir(path string) string {
|
||||
return path
|
||||
}
|
||||
return path[:idx]
|
||||
}
|
||||
}
|
||||
|
||||
func Base(path string) string {
|
||||
idx := strings.LastIndex(path, "/")
|
||||
if idx == -1 {
|
||||
return path
|
||||
}
|
||||
return path[idx+1:]
|
||||
}
|
||||
|
||||
func Join(elem ...string) string {
|
||||
res := path.Join(elem...)
|
||||
if res == "\\" {
|
||||
res = "/"
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GetMD5Encode
|
||||
@ -16,3 +17,11 @@ func GetMD5Encode(data string) string {
|
||||
func Get16MD5Encode(data string) string {
|
||||
return GetMD5Encode(data)[8:24]
|
||||
}
|
||||
|
||||
func SignWithPassword(name, password string) string {
|
||||
return Get16MD5Encode(fmt.Sprintf("alist-%s-%s", password, name))
|
||||
}
|
||||
|
||||
func SignWithToken(name, token string) string {
|
||||
return Get16MD5Encode(fmt.Sprintf("alist-%s-%s", token, name))
|
||||
}
|
||||
|
Reference in New Issue
Block a user