package repo import ( "errors" "io/ioutil" "path" "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, "/") if 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 } // ctx.Data["OriginName"] = attachment.Name } 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 } 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) }