From 6689c858d66b43d1a3d504bb892b8537f4067222 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Fri, 20 Nov 2020 14:01:29 +0100 Subject: [PATCH] expose some functions of file package, add more MIME types to file download function to cover result file formats --- routes/file/file_endpoints.go | 19 ++++++++++++++----- routes/file/file_methods.go | 6 +++--- routes/file/file_middleware.go | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/routes/file/file_endpoints.go b/routes/file/file_endpoints.go index ba52a9e..df1ae77 100644 --- a/routes/file/file_endpoints.go +++ b/routes/file/file_endpoints.go @@ -104,7 +104,7 @@ func addFile(c *gin.Context) { } var newFile File - err = newFile.register(file_header, so.ID) + err = newFile.Register(file_header, so.ID) if !helper.DBError(c, err) { c.JSON(http.StatusOK, gin.H{"file": newFile.File}) } @@ -116,6 +116,15 @@ func addFile(c *gin.Context) { // @ID getFile // @Tags files // @Produce text/plain +// @Produce text/csv +// @Produce application/gzip +// @Produce application/x-gtar +// @Produce application/x-tar +// @Produce application/x-ustar +// @Produce application/zip +// @Produce application/msexcel +// @Produce application/xml +// @Produce application/x-bag // @Produce png // @Produce jpeg // @Produce gif @@ -132,7 +141,7 @@ func addFile(c *gin.Context) { func getFile(c *gin.Context) { // check access - ok, f := checkPermissions(c, database.Read) + ok, f := CheckPermissions(c, database.Read) if !ok { return } @@ -165,7 +174,7 @@ func getFile(c *gin.Context) { func updateFile(c *gin.Context) { // check access - ok, f := checkPermissions(c, database.Update) + ok, f := CheckPermissions(c, database.Update) if !ok { return } @@ -199,12 +208,12 @@ func updateFile(c *gin.Context) { func deleteFile(c *gin.Context) { // check access - ok, f := checkPermissions(c, database.Delete) + ok, f := CheckPermissions(c, database.Delete) if !ok { return } - err := f.delete() + err := f.Delete() if !helper.DBError(c, err) { c.JSON(http.StatusOK, gin.H{"file": f.File}) } diff --git a/routes/file/file_methods.go b/routes/file/file_methods.go index 1694ede..2e3eae0 100644 --- a/routes/file/file_methods.go +++ b/routes/file/file_methods.go @@ -44,7 +44,7 @@ type File struct { database.File } -func (f *File) byID(id uint) error { +func (f *File) ByID(id uint) error { db := database.GetDB() err := db.Find(f, id).Error if err != nil { @@ -74,7 +74,7 @@ func (f *File) download(c *gin.Context) error { return nil } -func (f *File) register(fileHeader *multipart.FileHeader, scenarioID uint) error { +func (f *File) Register(fileHeader *multipart.FileHeader, scenarioID uint) error { // Obtain properties of file f.Type = fileHeader.Header.Get("Content-Type") @@ -179,7 +179,7 @@ func (f *File) update(fileHeader *multipart.FileHeader) error { return err } -func (f *File) delete() error { +func (f *File) Delete() error { db := database.GetDB() diff --git a/routes/file/file_middleware.go b/routes/file/file_middleware.go index 3adf779..acdacb6 100644 --- a/routes/file/file_middleware.go +++ b/routes/file/file_middleware.go @@ -29,7 +29,7 @@ import ( "github.com/gin-gonic/gin" ) -func checkPermissions(c *gin.Context, operation database.CRUD) (bool, File) { +func CheckPermissions(c *gin.Context, operation database.CRUD) (bool, File) { var f File @@ -44,7 +44,7 @@ func checkPermissions(c *gin.Context, operation database.CRUD) (bool, File) { return false, f } - err = f.byID(uint(fileID)) + err = f.ByID(uint(fileID)) if helper.DBError(c, err) { return false, f }