Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
f31fc9155a | |||
d3db012dd0 | |||
74b86ef5e5 | |||
6be90429ad | |||
a6bbff2199 | |||
3827d3ed67 | |||
e242867f41 | |||
e09a75a87c | |||
e0a80b1477 | |||
d693e27ec0 | |||
b20f0717fe | |||
427244d8d5 | |||
b613598c2b | |||
f0013320a6 | |||
974caf74d9 | |||
0bb02664c7 |
@ -78,4 +78,3 @@ func DoPost(url string, request interface{}, auth string) (body []byte, err erro
|
|||||||
log.Debugf("请求返回信息:%s", string(body))
|
log.Debugf("请求返回信息:%s", string(body))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
// list request bean
|
// ListReq list request bean
|
||||||
type ListReq struct {
|
type ListReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
Fields string `json:"fields"`
|
Fields string `json:"fields"`
|
||||||
@ -14,7 +14,7 @@ type ListReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// get request bean
|
// GetReq get request bean
|
||||||
type GetReq struct {
|
type GetReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
@ -22,7 +22,7 @@ type GetReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// download request bean
|
// DownloadReq download request bean
|
||||||
type DownloadReq struct {
|
type DownloadReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
@ -30,7 +30,7 @@ type DownloadReq struct {
|
|||||||
FileName string `json:"file_name"`
|
FileName string `json:"file_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// search request bean
|
// SearchReq search request bean
|
||||||
type SearchReq struct {
|
type SearchReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
||||||
@ -44,31 +44,32 @@ type SearchReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// token_login request bean
|
// TokenLoginReq token_login request bean
|
||||||
type TokenLoginReq struct {
|
type TokenLoginReq struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_token request bean
|
// GetTokenReq get_token request bean
|
||||||
type GetTokenReq struct {
|
type GetTokenReq struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh_token request bean
|
// RefreshTokenReq refresh_token request bean
|
||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
GrantType string `json:"grant_type"`
|
GrantType string `json:"grant_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// office_preview_url request bean
|
// OfficePreviewUrlReq office_preview_url request bean
|
||||||
type OfficePreviewUrlReq struct {
|
type OfficePreviewUrlReq struct {
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// video preview url request bean
|
// VideoPreviewUrlReq video preview url request bean
|
||||||
type VideoPreviewUrlReq struct {
|
type VideoPreviewUrlReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
ExpireSec int `json:"expire_sec"`
|
||||||
|
}
|
@ -112,12 +112,13 @@ func GetOfficePreviewUrl(fileId string, drive *conf.Drive) (*OfficePreviewUrlRes
|
|||||||
func GetVideoPreviewUrl(fileId string, drive *conf.Drive) (*VideoPreviewUrlResp, error) {
|
func GetVideoPreviewUrl(fileId string, drive *conf.Drive) (*VideoPreviewUrlResp, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/databox/get_video_play_info"
|
url := conf.Conf.AliDrive.ApiUrl + "/databox/get_video_play_info"
|
||||||
req := VideoPreviewUrlReq{
|
req := VideoPreviewUrlReq{
|
||||||
DriveId: drive.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
FileId: fileId,
|
FileId: fileId,
|
||||||
|
ExpireSec: 14400,
|
||||||
}
|
}
|
||||||
var resp VideoPreviewUrlResp
|
var resp VideoPreviewUrlResp
|
||||||
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,14 @@ type Path struct {
|
|||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 秒传
|
||||||
|
{
|
||||||
|
"name":"mikuclub.mp4",
|
||||||
|
"content_hash":"C733AC50D1F964C0398D0E403F3A30C37EFC2ADD",
|
||||||
|
"size":1141068377,
|
||||||
|
"content_type":"video/mp4"
|
||||||
|
}
|
||||||
|
*/
|
||||||
// file response bean
|
// file response bean
|
||||||
type File struct {
|
type File struct {
|
||||||
RespError
|
RespError
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -9,5 +10,8 @@ import (
|
|||||||
// init request client
|
// init request client
|
||||||
func InitClient() {
|
func InitClient() {
|
||||||
log.Infof("初始化client...")
|
log.Infof("初始化client...")
|
||||||
conf.Client = &http.Client{}
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
conf.Client = &http.Client{Transport: tr}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,9 @@ func ReadConf(config string) bool {
|
|||||||
log.Infof("读取配置文件...")
|
log.Infof("读取配置文件...")
|
||||||
if !utils.Exists(config) {
|
if !utils.Exists(config) {
|
||||||
log.Infof("找不到配置文件:%s", config)
|
log.Infof("找不到配置文件:%s", config)
|
||||||
return false
|
if !Write(config) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
confFile, err := ioutil.ReadFile(config)
|
confFile, err := ioutil.ReadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -31,3 +33,52 @@ func ReadConf(config string) bool {
|
|||||||
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
|
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
func Write(path string) bool {
|
||||||
|
log.Infof("创建默认配置文件")
|
||||||
|
file, err := utils.CreatNestedFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("无法创建配置文件, %s", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
str := `
|
||||||
|
info:
|
||||||
|
title: AList #标题
|
||||||
|
logo: "" #网站logo 如果填写,则会替换掉默认的
|
||||||
|
footer_text: Xhofe's Blog #网页底部文字
|
||||||
|
footer_url: https://www.nn.ci #网页底部文字链接
|
||||||
|
music_img: https://img.xhofe.top/2020/12/19/0f8b57866bdb5.gif #预览音乐文件时的图片
|
||||||
|
check_update: true #前端是否显示更新
|
||||||
|
script: #自定义脚本,可以是脚本的链接,也可以直接是脚本内容
|
||||||
|
autoplay: true #视频是否自动播放
|
||||||
|
preview:
|
||||||
|
text: [txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp] #要预览的文本文件的后缀,可以自行添加
|
||||||
|
server:
|
||||||
|
address: "0.0.0.0"
|
||||||
|
port: "5244"
|
||||||
|
search: true
|
||||||
|
static: dist
|
||||||
|
site_url: '*'
|
||||||
|
password: password #用于重建目录
|
||||||
|
ali_drive:
|
||||||
|
api_url: https://api.aliyundrive.com/v2
|
||||||
|
max_files_count: 100
|
||||||
|
drives:
|
||||||
|
- refresh_token: xxx #refresh_token
|
||||||
|
root_folder: root #根目录的file_id
|
||||||
|
name: drive0 #盘名,多个盘不可重复,这里只是示例,不是一定要叫这个名字,可随意修改
|
||||||
|
password: pass #该盘密码,空('')则不设密码,修改需要重建生效
|
||||||
|
hide: false #是否在主页隐藏该盘,不可全部隐藏,至少暴露一个
|
||||||
|
database:
|
||||||
|
type: sqlite3
|
||||||
|
dBFile: alist.db
|
||||||
|
`
|
||||||
|
_, err = file.WriteString(str)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("无法写入配置文件, %s", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -18,7 +18,7 @@ server:
|
|||||||
password: password #用于重建目录
|
password: password #用于重建目录
|
||||||
ali_drive:
|
ali_drive:
|
||||||
api_url: https://api.aliyundrive.com/v2
|
api_url: https://api.aliyundrive.com/v2
|
||||||
max_files_count: 3000
|
max_files_count: 50 #重建目录时每次请求的文件
|
||||||
drives:
|
drives:
|
||||||
- refresh_token: xxx #refresh_token
|
- refresh_token: xxx #refresh_token
|
||||||
root_folder: root #根目录的file_id
|
root_folder: root #根目录的file_id
|
||||||
|
@ -22,7 +22,7 @@ var (
|
|||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "v1.0.2"
|
VERSION = "v1.0.5"
|
||||||
|
|
||||||
ImageThumbnailProcess = "image/resize,w_50"
|
ImageThumbnailProcess = "image/resize,w_50"
|
||||||
VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,w_50"
|
VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,w_50"
|
||||||
|
@ -1,24 +1,25 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
type Response struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
// common meta response
|
// MetaResponse common meta response
|
||||||
func MetaResponse(code int, msg string) gin.H {
|
func MetaResponse(code int, msg string) Response {
|
||||||
return gin.H{
|
return Response{
|
||||||
"meta": gin.H{
|
Code: code,
|
||||||
"code": code,
|
Data: nil,
|
||||||
"msg": msg,
|
Message: msg,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// common data response
|
// DataResponse common data response
|
||||||
func DataResponse(data interface{}) gin.H {
|
func DataResponse(data interface{}) Response {
|
||||||
return gin.H{
|
return Response{
|
||||||
"meta": gin.H{
|
Code: 200,
|
||||||
"code": 200,
|
Data: data,
|
||||||
"msg": "success",
|
Message: "ok",
|
||||||
},
|
|
||||||
"data": data,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ func Down(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
drive := utils.GetDriveByName(strings.Split(dir, "/")[0])
|
drive := utils.GetDriveByName(strings.Split(filePath, "/")[0])
|
||||||
if drive == nil {
|
if drive == nil {
|
||||||
c.JSON(200, MetaResponse(500, "找不到drive."))
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
return
|
return
|
||||||
|
@ -42,7 +42,7 @@ func Get(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
drive := utils.GetDriveByName(strings.Split(dir, "/")[0])
|
drive := utils.GetDriveByName(strings.Split(get.Path, "/")[0])
|
||||||
if drive == nil {
|
if drive == nil {
|
||||||
c.JSON(200, MetaResponse(500, "找不到drive."))
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
return
|
return
|
||||||
|
@ -27,7 +27,7 @@ func Path(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// folder model not exist
|
// folder model not exist
|
||||||
if file == nil {
|
if file == nil {
|
||||||
c.JSON(200, MetaResponse(404, "path not found."))
|
c.JSON(200, MetaResponse(404, "path not found.(第一次请先点击网页底部rebuild)"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
@ -3,8 +3,8 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/server/models"
|
"github.com/Xhofe/alist/server/models"
|
||||||
"github.com/Xhofe/alist/utils"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle info request
|
// handle info request
|
||||||
@ -12,14 +12,21 @@ func Info(c *gin.Context) {
|
|||||||
c.JSON(200, DataResponse(conf.Conf.Info))
|
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RebuildReq struct {
|
||||||
|
Path string `json:"path" binding:"required"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Depth int `json:"depth"`
|
||||||
|
}
|
||||||
|
|
||||||
// rebuild tree
|
// rebuild tree
|
||||||
func RebuildTree(c *gin.Context) {
|
func RebuildTree(c *gin.Context) {
|
||||||
drive := utils.GetDriveByName(c.Param("drive"))
|
var req RebuildReq
|
||||||
if drive == nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(200, MetaResponse(400, "drive isn't exist."))
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
password := c.Param("password")
|
log.Debugf("rebuild:%+v", req)
|
||||||
|
password := req.Password
|
||||||
if password != conf.Conf.Server.Password {
|
if password != conf.Conf.Server.Password {
|
||||||
if password == "" {
|
if password == "" {
|
||||||
c.JSON(200, MetaResponse(401, "need password."))
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
@ -28,11 +35,7 @@ func RebuildTree(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(401, "wrong password."))
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := models.Clear(drive); err != nil {
|
if err := models.BuildTreeWithPath(req.Path, req.Depth); err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := models.BuildTree(drive); err != nil {
|
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -4,23 +4,25 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildTreeAll() {
|
func BuildTreeAll(depth int) {
|
||||||
for i, _ := range conf.Conf.AliDrive.Drives {
|
for i, _ := range conf.Conf.AliDrive.Drives {
|
||||||
if err := BuildTree(&conf.Conf.AliDrive.Drives[i]); err != nil {
|
if err := BuildTree(&conf.Conf.AliDrive.Drives[i], depth); err != nil {
|
||||||
log.Errorf("盘[%s]构建目录树失败:%s", err.Error())
|
log.Errorf("盘[%s]构建目录树失败:%s", conf.Conf.AliDrive.Drives[i].Name, err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Infof("盘[%s]构建目录树成功")
|
log.Infof("盘[%s]构建目录树成功", conf.Conf.AliDrive.Drives[i].Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// build tree
|
// build tree
|
||||||
func BuildTree(drive *conf.Drive) error {
|
func BuildTree(drive *conf.Drive, depth int) error {
|
||||||
log.Infof("开始构建目录树...")
|
log.Infof("开始构建目录树...")
|
||||||
tx := conf.DB.Begin()
|
tx := conf.DB.Begin()
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -42,50 +44,162 @@ func BuildTree(drive *conf.Drive) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := BuildOne(drive.RootFolder, drive.Name+"/", tx, drive.Password, drive); err != nil {
|
if err := BuildOne(drive.RootFolder, drive.Name+"/", tx, drive.Password, drive, depth); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tx.Commit().Error
|
return tx.Commit().Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string, drive *conf.Drive) error {
|
/*
|
||||||
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "", drive)
|
递归构建目录树,插入指定目录下的所有文件
|
||||||
if err != nil {
|
parent 父目录的file_id
|
||||||
return err
|
path 指定的目录
|
||||||
|
parentPassword 父目录所携带的密码
|
||||||
|
drive 要构建的盘
|
||||||
|
*/
|
||||||
|
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string, drive *conf.Drive, depth int) error {
|
||||||
|
if depth == 0 {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
for _, file := range files.Items {
|
marker := "first"
|
||||||
name := file.Name
|
for marker != "" {
|
||||||
if strings.HasSuffix(name, ".hide") {
|
if marker == "first" {
|
||||||
continue
|
marker = ""
|
||||||
}
|
}
|
||||||
password := parentPassword
|
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, marker, "", "", drive)
|
||||||
if strings.Contains(name, ".password-") {
|
if err != nil {
|
||||||
index := strings.Index(name, ".password-")
|
|
||||||
name = file.Name[:index]
|
|
||||||
password = file.Name[index+10:]
|
|
||||||
}
|
|
||||||
newFile := File{
|
|
||||||
Dir: path,
|
|
||||||
FileExtension: file.FileExtension,
|
|
||||||
FileId: file.FileId,
|
|
||||||
Name: name,
|
|
||||||
Type: file.Type,
|
|
||||||
UpdatedAt: file.UpdatedAt,
|
|
||||||
Category: file.Category,
|
|
||||||
ContentType: file.ContentType,
|
|
||||||
Size: file.Size,
|
|
||||||
Password: password,
|
|
||||||
}
|
|
||||||
log.Debugf("插入file:%+v", newFile)
|
|
||||||
if err := tx.Create(&newFile).Error; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if file.Type == "folder" {
|
marker = files.NextMarker
|
||||||
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive); err != nil {
|
for _, file := range files.Items {
|
||||||
|
name := file.Name
|
||||||
|
password := parentPassword
|
||||||
|
if strings.HasSuffix(name, ".hide") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Contains(name, ".ln-") {
|
||||||
|
index := strings.Index(name, ".ln-")
|
||||||
|
name = file.Name[:index]
|
||||||
|
fileId := file.Name[index+4:]
|
||||||
|
newFile := File{
|
||||||
|
Dir: path,
|
||||||
|
FileExtension: "",
|
||||||
|
FileId: fileId,
|
||||||
|
Name: name,
|
||||||
|
Type: "folder",
|
||||||
|
UpdatedAt: file.UpdatedAt,
|
||||||
|
Category: "",
|
||||||
|
ContentType: "",
|
||||||
|
Size: 0,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
log.Debugf("插入file:%+v", newFile)
|
||||||
|
if err = tx.Create(&newFile).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = BuildOne(fileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive, depth-1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Contains(name, ".password-") {
|
||||||
|
index := strings.Index(name, ".password-")
|
||||||
|
name = file.Name[:index]
|
||||||
|
password = file.Name[index+10:]
|
||||||
|
}
|
||||||
|
newFile := File{
|
||||||
|
Dir: path,
|
||||||
|
FileExtension: file.FileExtension,
|
||||||
|
FileId: file.FileId,
|
||||||
|
Name: name,
|
||||||
|
Type: file.Type,
|
||||||
|
UpdatedAt: file.UpdatedAt,
|
||||||
|
Category: file.Category,
|
||||||
|
ContentType: file.ContentType,
|
||||||
|
Size: file.Size,
|
||||||
|
Password: password,
|
||||||
|
ContentHash: file.ContentHash,
|
||||||
|
}
|
||||||
|
log.Debugf("插入file:%+v", newFile)
|
||||||
|
if err := tx.Create(&newFile).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if file.Type == "folder" {
|
||||||
|
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive, depth-1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//重建指定路径与深度的目录树: 先删除该目录与该目录下所有文件的model,再重新插入
|
||||||
|
func BuildTreeWithPath(path string, depth int) error {
|
||||||
|
dir, name := filepath.Split(path)
|
||||||
|
driveName := strings.Split(path, "/")[0]
|
||||||
|
drive := utils.GetDriveByName(driveName)
|
||||||
|
if drive == nil {
|
||||||
|
return fmt.Errorf("找不到drive[%s]", driveName)
|
||||||
|
}
|
||||||
|
file := &File{
|
||||||
|
Dir: "",
|
||||||
|
FileId: drive.RootFolder,
|
||||||
|
Name: drive.Name,
|
||||||
|
Type: "folder",
|
||||||
|
Password: drive.Password,
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if dir != "" {
|
||||||
|
file, err = GetFileByDirAndName(dir, name)
|
||||||
|
if err != nil {
|
||||||
|
if file == nil {
|
||||||
|
return fmt.Errorf("path not found")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tx := conf.DB.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err = tx.Error; err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = tx.Where("dir = ? AND name = ?", file.Dir, file.Name).Delete(file).Error; err != nil{
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = tx.Where("dir like ?", fmt.Sprintf("%s%%", path)).Delete(&File{}).Error; err != nil{
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//if dir != "" {
|
||||||
|
// aliFile, err := alidrive.GetFile(file.FileId, drive)
|
||||||
|
// if err != nil {
|
||||||
|
// tx.Rollback()
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// aliName := aliFile.Name
|
||||||
|
// if strings.HasSuffix(aliName, ".hide") {
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// if strings.Contains(aliName, ".password-") {
|
||||||
|
// index := strings.Index(name, ".password-")
|
||||||
|
// file.Name = aliName[:index]
|
||||||
|
// file.Password = aliName[index+10:]
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
if err = tx.Create(&file).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = BuildOne(file.FileId, path+"/", tx, file.Password, drive, depth); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit().Error
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ type File struct {
|
|||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Url string `json:"url" gorm:"-"`
|
Url string `json:"url" gorm:"-"`
|
||||||
|
ContentHash string `json:"content_hash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (file *File) Create() error {
|
func (file *File) Create() error {
|
||||||
@ -62,3 +63,7 @@ func SearchByNameInDir(keyword string, dir string) (*[]File, error) {
|
|||||||
}
|
}
|
||||||
return &files, nil
|
return &files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteWithDir(dir string) error {
|
||||||
|
return conf.DB.Where("dir like ?", fmt.Sprintf("%s%%", dir)).Delete(&File{}).Error
|
||||||
|
}
|
@ -30,7 +30,7 @@ func InitApiRouter(engine *gin.Engine) {
|
|||||||
apiV2.POST("/video_preview/:drive", controllers.VideoPreview)
|
apiV2.POST("/video_preview/:drive", controllers.VideoPreview)
|
||||||
apiV2.POST("/local_search", controllers.LocalSearch)
|
apiV2.POST("/local_search", controllers.LocalSearch)
|
||||||
apiV2.POST("/global_search", controllers.GlobalSearch)
|
apiV2.POST("/global_search", controllers.GlobalSearch)
|
||||||
apiV2.GET("/rebuild/:drive/:password", controllers.RebuildTree)
|
apiV2.POST("/rebuild", controllers.RebuildTree)
|
||||||
}
|
}
|
||||||
engine.GET("/d/*path", controllers.Down)
|
engine.GET("/d/*path", controllers.Down)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user