diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index 3a5f514c7..2c1c44814 100755 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -13,16 +13,11 @@ import ( "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/worker" contexExt "context" - "encoding/json" "fmt" - "io/ioutil" + gouuid "github.com/satori/go.uuid" "net/http" - "path" "strconv" "strings" - "time" - - gouuid "github.com/satori/go.uuid" ) const ( @@ -31,12 +26,6 @@ const ( DecompressFailed = "1" ) -type FileInfo struct { - FileName string - ModTime time.Time - IsDir bool -} - func RenderAttachmentSettings(ctx *context.Context) { renderAttachmentSettings(ctx) } @@ -360,44 +349,3 @@ func HandleUnDecompressAttachment() { return } - -func GetDir(ctx *context.Context) { - uuid := ctx.Query("uuid") - - //todo: 查询解压状态成功之后才能查看文件目录 - files, err := ioutil.ReadDir(setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.Attachment.Minio.BasePath + - path.Join(uuid[0:1], uuid[1:2], uuid + uuid) + "/" + ctx.Query("parentDir")) - if err != nil { - log.Error("ReadDir failed:", err.Error()) - ctx.ServerError("ReadDir failed:", err) - return - } - - i := 1 - var fileInfos []FileInfo - for _, file := range files { - if i > 100 { - break - } - - fileInfos = append(fileInfos, FileInfo{ - FileName:file.Name(), - ModTime:file.ModTime(), - IsDir:file.IsDir(), - }) - i++ - } - - tmp, err := json.Marshal(fileInfos) - if err != nil { - ctx.ServerError("json.Marshal failed:", err) - return - } - - log.Info(string(tmp)) - - ctx.JSON(200, map[string]string{ - "uuid": uuid, - "fileInfos": string(tmp), - }) -} diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go old mode 100644 new mode 100755 diff --git a/routers/repo/dir.go b/routers/repo/dir.go new file mode 100755 index 000000000..dd7612f57 --- /dev/null +++ b/routers/repo/dir.go @@ -0,0 +1,138 @@ +package repo + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + "encoding/json" + "errors" + "io/ioutil" + "path" + "strings" +) + +const ( + tplDirIndex base.TplName = "repo/datasets/dirs/index" +) + +type FileInfo struct { + FileName string + ModTime string + IsDir bool + Size int64 +} + +func DirIndex(ctx *context.Context) { + uuid := ctx.Params("uuid") + + attachment, err := models.GetAttachmentByUUID(uuid) + if err != nil { + ctx.ServerError("GetDatasetAttachments", err) + return + } + + if !strings.HasSuffix(attachment.Name, ".zip") { + log.Error("The file is not zip file, can not query the dir") + ctx.ServerError("The file is not zip file, can not query the dir", errors.New("The file is not zip file, can not query the dir")) + return + } else { + if attachment.DecompressState != models.DecompressStateDone { + log.Error("The file has not been decompressed completely now") + ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now")) + return + } + } + + files, err := ioutil.ReadDir(setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.Attachment.Minio.BasePath + + path.Join(uuid[0:1], uuid[1:2], uuid + uuid) + "/" ) + if err != nil { + log.Error("ReadDir failed:", err.Error()) + ctx.ServerError("ReadDir failed:", err) + return + } + + i := 1 + var fileInfos []FileInfo + for _, file := range files { + if i > 100 { + break + } + + fileInfos = append(fileInfos, FileInfo{ + FileName:file.Name(), + ModTime:file.ModTime().Format("2006-01-02 15:04:05"), + IsDir:file.IsDir(), + Size:file.Size(), + }) + i++ + } + + ctx.Data["Dirs"] = fileInfos + + ctx.Data["Title"] = attachment.Name + + ctx.HTML(200, tplDirIndex) +} + +func GetDir(ctx *context.Context) { + uuid := ctx.Query("uuid") + parentDir := ctx.Query("parentDir") + + if parentDir == "" { + attachment, err := models.GetAttachmentByUUID(uuid) + if err != nil { + ctx.ServerError("GetAttachmentByUUID failed:", err) + return + } + + if !strings.HasSuffix(attachment.Name, ".zip") { + log.Error("The file is not zip file, can not query the dir") + ctx.ServerError("The file is not zip file, can not query the dir", errors.New("The file is not zip file, can not query the dir")) + return + } else { + if attachment.DecompressState != models.DecompressStateDone { + log.Error("The file has not been decompressed completely now") + ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now")) + return + } + } + } + + files, err := ioutil.ReadDir(setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.Attachment.Minio.BasePath + + path.Join(uuid[0:1], uuid[1:2], uuid + uuid) + "/" + parentDir) + if err != nil { + log.Error("ReadDir failed:", err.Error()) + ctx.ServerError("ReadDir failed:", err) + return + } + + i := 1 + var fileInfos []FileInfo + for _, file := range files { + if i > 100 { + break + } + + fileInfos = append(fileInfos, FileInfo{ + FileName:file.Name(), + ModTime:file.ModTime().Format("2006-01-02 15:04:05"), + IsDir:file.IsDir(), + }) + i++ + } + + tmp, err := json.Marshal(fileInfos) + if err != nil { + ctx.ServerError("json.Marshal failed:", err) + return + } + + ctx.Data["Dirs"] = fileInfos + + ctx.JSON(200, map[string]string{ + "uuid": uuid, + "fileInfos": string(tmp), + }) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index ce941dfcb..27854697d 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -521,7 +521,6 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/get_pre_url", repo.GetPresignedPutObjectURL) m.Post("/add", repo.AddAttachment) m.Post("/private", repo.UpdatePublicAttachment) - m.Get("/dir", repo.GetDir) }, reqSignIn) m.Group("/attachments", func() { @@ -874,6 +873,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/datasets", func() { m.Get("", reqRepoDatasetReader, repo.DatasetIndex) m.Post("", reqRepoDatasetWriter, bindIgnErr(auth.EditDatasetForm{}), repo.EditDatasetPost) + + m.Group("/dirs/:uuid", func() { + m.Get("", reqRepoDatasetReader, repo.DirIndex) + }) }, context.RepoRef()) m.Group("/cloudbrain", func() { diff --git a/templates/repo/datasets/dataset_list.tmpl b/templates/repo/datasets/dataset_list.tmpl index c6b43036c..e2c1ef609 100755 --- a/templates/repo/datasets/dataset_list.tmpl +++ b/templates/repo/datasets/dataset_list.tmpl @@ -19,7 +19,7 @@
- {{$.i18n.Tr "dataset.dir"}} + {{$.i18n.Tr "dataset.dir"}}
{{if $.Permission.CanWrite $.UnitTypeDatasets}} diff --git a/templates/repo/datasets/dirs/dir_list.tmpl b/templates/repo/datasets/dirs/dir_list.tmpl new file mode 100755 index 000000000..e435ed785 --- /dev/null +++ b/templates/repo/datasets/dirs/dir_list.tmpl @@ -0,0 +1,20 @@ +{{if .Dirs}} + {{range .Dirs}} +
+
+ +
+ {{.Size | FileSize}} +
+
+ {{.ModTime}} +
+ +
+
+ {{end}} +{{end}} diff --git a/templates/repo/datasets/dirs/index.tmpl b/templates/repo/datasets/dirs/index.tmpl new file mode 100755 index 000000000..b90c924cc --- /dev/null +++ b/templates/repo/datasets/dirs/index.tmpl @@ -0,0 +1,33 @@ +{{template "base/head" .}} + +
+
+
+
+
+

{{.Title}}

+
+
+
+
+
+ +
+ +
+
+
+
+
+
+

{{.i18n.Tr "dataset.dir"}}

+
+
+
+
+ {{template "repo/datasets/dirs/dir_list" .}} +
+
+
+ +{{template "base/footer" .}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl old mode 100644 new mode 100755 diff --git a/web_src/js/index.js b/web_src/js/index.js index 0776a7a9e..60dfb231d 100755 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -1130,19 +1130,17 @@ async function initRepository() { } }).modal('show'); }); - $('[data-dataset-dir]').on('click', function () { + /*$('[data-dataset-dir]').on('click', function () { const $this = $(this); $.get($this.data('dir-url'), { _csrf: $this.data('csrf'), - uuid: $this.data('uuid'), - //parentDir: $this.data('parentDir') - parentDir: "test" + uuid: $this.data('uuid') }).done((_data) => { $(`#${$this.data('uuid')}`).hide(); }).fail(() => { //window.location.reload(); }); - }); + });*/ $('[data-category-id]').on('click', function () { const category = $(this).data('category-id'); $('#category').val(category);