Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
hexbee
Cloudreve
提交
a89c3cb1
C
Cloudreve
项目概览
hexbee
/
Cloudreve
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cloudreve
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a89c3cb1
编写于
11月 24, 2019
作者:
H
HFO4
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Feat: create folder / Modify: add index to some column
上级
89b54e40
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
112 addition
and
9 deletion
+112
-9
models/file.go
models/file.go
+2
-2
models/folder.go
models/folder.go
+14
-5
pkg/filesystem/path.go
pkg/filesystem/path.go
+37
-0
pkg/serializer/common.go
pkg/serializer/common.go
+2
-0
routers/controllers/directory.go
routers/controllers/directory.go
+16
-0
routers/router.go
routers/router.go
+2
-2
service/explorer/directory.go
service/explorer/directory.go
+39
-0
未找到文件。
models/file.go
浏览文件 @
a89c3cb1
...
...
@@ -11,10 +11,10 @@ type File struct {
gorm
.
Model
Name
string
SourceName
string
UserID
uint
UserID
uint
`gorm:"index:user_id"`
Size
uint64
PicInfo
string
FolderID
uint
FolderID
uint
`gorm:"index:folder_id"`
PolicyID
uint
Dir
string
`gorm:"size:65536"`
}
...
...
models/folder.go
浏览文件 @
a89c3cb1
package
model
import
"github.com/jinzhu/gorm"
import
(
"github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm"
)
// Folder 目录
type
Folder
struct
{
// 表字段
gorm
.
Model
Name
string
ParentID
uint
ParentID
uint
`gorm:"index:parent_id"`
Position
string
`gorm:"size:65536"`
OwnerID
uint
OwnerID
uint
`gorm:"index:owner_id"`
PositionAbsolute
string
`gorm:"size:65536"`
}
// 关联模型
OptionsSerialized
PolicyOption
`gorm:"-"`
// Create 创建目录
func
(
folder
*
Folder
)
Create
()
(
uint
,
error
)
{
if
err
:=
DB
.
Create
(
folder
)
.
Error
;
err
!=
nil
{
util
.
Log
()
.
Warning
(
"无法插入目录记录, %s"
,
err
)
return
0
,
err
}
return
folder
.
ID
,
nil
}
// GetFolderByPath 根据绝对路径和UID查找目录
...
...
pkg/filesystem/path.go
浏览文件 @
a89c3cb1
package
filesystem
import
(
"context"
model
"github.com/HFO4/cloudreve/models"
"path"
)
...
...
@@ -10,6 +11,42 @@ import (
=================
*/
// CreateDirectory 在`base`路径下创建名为`dir`的目录
func
(
fs
*
FileSystem
)
CreateDirectory
(
ctx
context
.
Context
,
fullPath
string
)
error
{
// 获取要创建目录的父路径和目录名
fullPath
=
path
.
Clean
(
fullPath
)
base
:=
path
.
Dir
(
fullPath
)
dir
:=
path
.
Base
(
fullPath
)
// 检查目录名是否合法
if
!
fs
.
ValidateLegalName
(
ctx
,
dir
)
{
return
ErrIllegalObjectName
}
// 父目录是否存在
isExist
,
parent
:=
fs
.
IsPathExist
(
base
)
if
!
isExist
{
return
ErrPathNotExist
}
// 是否有同名文件
if
fs
.
IsFileExist
(
path
.
Join
(
base
,
dir
))
{
return
ErrFileExisted
}
// 创建目录
newFolder
:=
model
.
Folder
{
Name
:
dir
,
ParentID
:
parent
.
ID
,
Position
:
base
,
OwnerID
:
fs
.
User
.
ID
,
PositionAbsolute
:
fullPath
,
}
_
,
err
:=
newFolder
.
Create
()
return
err
}
// IsPathExist 返回给定目录是否存在
// 如果存在就返回目录
func
(
fs
*
FileSystem
)
IsPathExist
(
path
string
)
(
bool
,
*
model
.
Folder
)
{
...
...
pkg/serializer/common.go
浏览文件 @
a89c3cb1
...
...
@@ -21,6 +21,8 @@ const (
CodeNoRightErr
=
403
// CodeUploadFailed 上传出错
CodeUploadFailed
=
4001
// CodeCreateFolderFailed 目录创建失败
CodeCreateFolderFailed
=
4002
// CodeDBError 数据库操作失败
CodeDBError
=
50001
// CodeEncryptError 加密失败
...
...
routers/controllers/directory.go
浏览文件 @
a89c3cb1
package
controllers
import
(
"github.com/HFO4/cloudreve/service/explorer"
"github.com/gin-gonic/gin"
)
// CreateDirectory 创建目录
func
CreateDirectory
(
c
*
gin
.
Context
)
{
var
service
explorer
.
DirectoryCreateService
if
err
:=
c
.
ShouldBindJSON
(
&
service
);
err
==
nil
{
res
:=
service
.
CreateDirectory
(
c
)
c
.
JSON
(
200
,
res
)
}
else
{
c
.
JSON
(
200
,
ErrorResponse
(
err
))
}
}
routers/router.go
浏览文件 @
a89c3cb1
...
...
@@ -67,8 +67,8 @@ func InitRouter() *gin.Engine {
// 目录
directory
:=
auth
.
Group
(
"directory"
)
{
//
文件上传
directory
.
PUT
(
""
,
controllers
.
Ping
)
//
创建目录
directory
.
PUT
(
""
,
controllers
.
CreateDirectory
)
}
}
...
...
service/explorer/directory.go
0 → 100644
浏览文件 @
a89c3cb1
package
explorer
import
(
"context"
model
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
)
// DirectoryCreateService 创建新目录服务
type
DirectoryCreateService
struct
{
Path
string
`form:"newPath" json:"newPath" binding:"required,min=1,max=65535"`
}
// CreateDirectory 创建目录
func
(
service
*
DirectoryCreateService
)
CreateDirectory
(
c
*
gin
.
Context
)
serializer
.
Response
{
// 创建文件系统
user
,
_
:=
c
.
Get
(
"user"
)
fs
,
err
:=
filesystem
.
NewFileSystem
(
user
.
(
*
model
.
User
))
if
err
!=
nil
{
return
serializer
.
Err
(
serializer
.
CodePolicyNotAllowed
,
err
.
Error
(),
err
)
}
// 上下文
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
// 创建目录
err
=
fs
.
CreateDirectory
(
ctx
,
service
.
Path
)
if
err
!=
nil
{
return
serializer
.
Err
(
serializer
.
CodeCreateFolderFailed
,
err
.
Error
(),
err
)
}
return
serializer
.
Response
{
Code
:
0
,
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录