feat: fixing article files upload + viewer
This commit is contained in:
parent
3b05b665c3
commit
7e9041d58b
|
|
@ -13,7 +13,7 @@ type ArticleFiles struct {
|
|||
WidthPixel *string `json:"width_pixel" gorm:"type:varchar"`
|
||||
HeightPixel *string `json:"height_pixel" gorm:"type:varchar"`
|
||||
Size *string `json:"size" gorm:"type:varchar"`
|
||||
DownloadCount *int `json:"download_count" gorm:"type:int4"`
|
||||
DownloadCount *int `json:"download_count" gorm:"type:int4;default:0"`
|
||||
CreatedById int `json:"created_by_id" gorm:"type:int4"`
|
||||
StatusId int `json:"status_id" gorm:"type:int4"`
|
||||
IsPublish *bool `json:"is_publish" gorm:"type:bool;default:false"`
|
||||
|
|
|
|||
|
|
@ -49,6 +49,6 @@ func (_i *ArticleFilesRouter) RegisterArticleFilesRoutes() {
|
|||
router.Post("/:articleId", articleFilesController.Save)
|
||||
router.Put("/:id", articleFilesController.Update)
|
||||
router.Delete("/:id", articleFilesController.Delete)
|
||||
router.Get("/viewer/:id", articleFilesController.Viewer)
|
||||
router.Get("/viewer/:filename", articleFilesController.Viewer)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -195,16 +195,12 @@ func (_i *articleFilesController) Delete(c *fiber.Ctx) error {
|
|||
// @Description API for create ArticleFiles
|
||||
// @Tags Article Files
|
||||
// @Security Bearer
|
||||
// @Param id path string true "Article File ID"
|
||||
// @Param filename path string true "Article File Name"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 400 {object} response.BadRequestError
|
||||
// @Failure 401 {object} response.UnauthorizedError
|
||||
// @Failure 500 {object} response.InternalServerError
|
||||
// @Router /article-files/viewer/{id} [get]
|
||||
// @Router /article-files/viewer/{filename} [get]
|
||||
func (_i *articleFilesController) Viewer(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return _i.articleFilesService.Viewer(c, uint(id))
|
||||
return _i.articleFilesService.Viewer(c)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ type ArticleFilesRepository interface {
|
|||
GetAll(req request.ArticleFilesQueryRequest) (articleFiless []*entity.ArticleFiles, paging paginator.Pagination, err error)
|
||||
FindOne(id uint) (articleFiles *entity.ArticleFiles, err error)
|
||||
FindByArticle(articleId uint) (articleFiles []*entity.ArticleFiles, err error)
|
||||
FindByFilename(filename string) (articleFiles *entity.ArticleFiles, err error)
|
||||
Create(articleFiles *entity.ArticleFiles) (err error)
|
||||
Update(id uint, articleFiles *entity.ArticleFiles) (err error)
|
||||
Delete(id uint) (err error)
|
||||
|
|
@ -88,6 +89,15 @@ func (_i *articleFilesRepository) FindByArticle(articleId uint) (articleFiles []
|
|||
return articleFiles, nil
|
||||
}
|
||||
|
||||
func (_i *articleFilesRepository) FindByFilename(articleFilename string) (articleFiles *entity.ArticleFiles, err error) {
|
||||
|
||||
if err := _i.DB.DB.Where("file_name = ?", articleFilename).First(&articleFiles).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return articleFiles, nil
|
||||
}
|
||||
|
||||
func (_i *articleFilesRepository) Create(articleFiles *entity.ArticleFiles) (err error) {
|
||||
return _i.DB.DB.Create(articleFiles).Error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import (
|
|||
"go-humas-be/utils/paginator"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
|
@ -34,7 +35,7 @@ type ArticleFilesService interface {
|
|||
Save(c *fiber.Ctx, id uint) error
|
||||
Update(id uint, req request.ArticleFilesUpdateRequest) (err error)
|
||||
Delete(id uint) error
|
||||
Viewer(c *fiber.Ctx, id uint) error
|
||||
Viewer(c *fiber.Ctx) error
|
||||
}
|
||||
|
||||
// NewArticleFilesService init ArticleFilesService
|
||||
|
|
@ -102,15 +103,26 @@ func (_i *articleFilesService) Save(c *fiber.Ctx, id uint) (err error) {
|
|||
}
|
||||
defer src.Close()
|
||||
|
||||
objectName := "articles/upload/" + file.Filename
|
||||
filenameOnly := file.Filename[:len(file.Filename)-len(filepath.Ext(file.Filename))]
|
||||
filename := filepath.Base(file.Filename)
|
||||
filenameAlt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
||||
filename = strings.ReplaceAll(filename, " ", "")
|
||||
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
||||
extension := filepath.Ext(file.Filename)[1:]
|
||||
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
randUniqueId := rand.Intn(1000000)
|
||||
|
||||
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
||||
newFilename := newFilenameWithoutExt + "." + extension
|
||||
|
||||
objectName := "articles/upload/" + newFilename
|
||||
fileSize := strconv.FormatInt(file.Size, 10)
|
||||
|
||||
req := request.ArticleFilesCreateRequest{
|
||||
ArticleId: id,
|
||||
FilePath: &objectName,
|
||||
FileName: &file.Filename,
|
||||
FileAlt: &filenameOnly,
|
||||
FileName: &newFilename,
|
||||
FileAlt: &filenameAlt,
|
||||
Size: &fileSize,
|
||||
}
|
||||
|
||||
|
|
@ -147,8 +159,9 @@ func (_i *articleFilesService) Delete(id uint) error {
|
|||
return _i.Repo.Update(id, result)
|
||||
}
|
||||
|
||||
func (_i *articleFilesService) Viewer(c *fiber.Ctx, id uint) (err error) {
|
||||
result, err := _i.Repo.FindOne(id)
|
||||
func (_i *articleFilesService) Viewer(c *fiber.Ctx) (err error) {
|
||||
filename := c.Params("filename")
|
||||
result, err := _i.Repo.FindByFilename(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ func (_i *articlesService) SaveThumbnail(c *fiber.Ctx) (err error) {
|
|||
defer src.Close()
|
||||
|
||||
filename := filepath.Base(file.Filename)
|
||||
filename = strings.ReplaceAll(filename, " ", "")
|
||||
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
||||
extension := filepath.Ext(file.Filename)[1:]
|
||||
|
||||
|
|
|
|||
|
|
@ -149,6 +149,7 @@ func (_i *ppidDataCategoriesService) SaveThumbnail(c *fiber.Ctx) (err error) {
|
|||
defer src.Close()
|
||||
|
||||
filename := filepath.Base(file.Filename)
|
||||
filename = strings.ReplaceAll(filename, " ", "")
|
||||
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
||||
extension := filepath.Ext(file.Filename)[1:]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue