|
- package repo
-
- import (
- "errors"
- "os"
- "path"
- "sort"
- "strings"
-
- "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"
- )
-
- const (
- tplDirIndex base.TplName = "repo/datasets/dirs/index"
- )
-
- type FileInfo struct {
- FileName string
- ModTime string
- IsDir bool
- Size int64
- ParenDir string
- UUID string
- }
-
- func DirIndex(ctx *context.Context) {
- uuid := ctx.Params("uuid")
- parentDir := ctx.Query("parentDir")
- dirArray := strings.Split(parentDir, "/")
-
- 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
- }
-
- dirArray = append([]string{attachment.Name}, dirArray...)
- if parentDir == "" {
- dirArray = []string{attachment.Name}
- }
-
- files, err := 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
- }
-
- log.Info(file.Name())
-
- var tmp string
- if parentDir == "" {
- tmp = file.Name()
- } else {
- tmp = parentDir + "/" + file.Name()
- }
-
- fileInfos = append(fileInfos, FileInfo{
- FileName: file.Name(),
- ModTime: file.ModTime().Format("2006-01-02 15:04:05"),
- IsDir: file.IsDir(),
- Size: file.Size(),
- ParenDir: tmp,
- UUID: uuid,
- })
- i++
- }
-
- ctx.Data["Path"] = dirArray
- ctx.Data["Dirs"] = fileInfos
- ctx.Data["PageIsDataset"] = true
-
- ctx.HTML(200, tplDirIndex)
- }
-
- // readDir reads the directory named by dirname and returns
- // a list of directory entries sorted by filename.
- func readDir(dirname string) ([]os.FileInfo, error) {
- f, err := os.Open(dirname)
- if err != nil {
- return nil, err
- }
- list, err := f.Readdir(100)
- f.Close()
- if err != nil {
- return nil, err
- }
- sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
- return list, nil
- }
|