feat: add media type to media model
This commit is contained in:
@@ -8,9 +8,32 @@ import (
|
|||||||
"quyun/database/schemas/public/table"
|
"quyun/database/schemas/public/table"
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
"github.com/samber/lo"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MediaType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
MediaTypeUnknown MediaType = "unknown"
|
||||||
|
MediaTypeArchive MediaType = "archive"
|
||||||
|
MediaTypeImage MediaType = "image"
|
||||||
|
MediaTypeVideo MediaType = "video"
|
||||||
|
MediaTypeDocument MediaType = "document"
|
||||||
|
MediaTypeAudio MediaType = "audio"
|
||||||
|
MediaTypePDF MediaType = "pdf"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MediaItem struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
UploadTime string `json:"upload_time"`
|
||||||
|
FileSize int64 `json:"file_size"`
|
||||||
|
MimeType string `json:"media_type"`
|
||||||
|
FileType MediaType `json:"file_type"`
|
||||||
|
ThumbnailUrl string `json:"thumbnail_url"`
|
||||||
|
}
|
||||||
|
|
||||||
// @provider
|
// @provider
|
||||||
type mediasModel struct {
|
type mediasModel struct {
|
||||||
log *logrus.Entry `inject:"false"`
|
log *logrus.Entry `inject:"false"`
|
||||||
@@ -65,8 +88,21 @@ func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert model.Medias to MediaItem
|
||||||
|
mediaItems := lo.Map(medias, func(media model.Medias, _ int) MediaItem {
|
||||||
|
return MediaItem{
|
||||||
|
ID: media.ID,
|
||||||
|
Name: media.Name,
|
||||||
|
UploadTime: media.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
|
FileSize: media.Size,
|
||||||
|
MimeType: media.MimeType,
|
||||||
|
FileType: m.ConvertFileTypeByMimeType(media.MimeType),
|
||||||
|
ThumbnailUrl: "",
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return &requests.Pager{
|
return &requests.Pager{
|
||||||
Items: medias,
|
Items: mediaItems,
|
||||||
Total: count,
|
Total: count,
|
||||||
Pagination: *pagination,
|
Pagination: *pagination,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -84,3 +120,33 @@ func (m *mediasModel) Create(ctx context.Context, model *model.Medias) error {
|
|||||||
m.log.Infof("media item created successfully")
|
m.log.Infof("media item created successfully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mediasModel) ConvertFileTypeByMimeType(mimeType string) MediaType {
|
||||||
|
switch mimeType {
|
||||||
|
case "image/jpeg":
|
||||||
|
case "image/jpg":
|
||||||
|
case "image/png":
|
||||||
|
return MediaTypeImage
|
||||||
|
case "video/mp4":
|
||||||
|
return MediaTypeVideo
|
||||||
|
case "audio/mpeg":
|
||||||
|
return MediaTypeAudio
|
||||||
|
case "application/pdf":
|
||||||
|
return MediaTypePDF
|
||||||
|
case "application/msword":
|
||||||
|
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||||
|
case "application/vnd.ms-excel":
|
||||||
|
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
||||||
|
case "application/vnd.ms-powerpoint":
|
||||||
|
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
||||||
|
return MediaTypeDocument
|
||||||
|
case "application/rar":
|
||||||
|
case "application/x-rar-compressed":
|
||||||
|
case "application/x-zip-compressed":
|
||||||
|
case "application/x-zip":
|
||||||
|
case "application/zip":
|
||||||
|
case "application/x-7z-compressed":
|
||||||
|
return MediaTypeArchive
|
||||||
|
}
|
||||||
|
return MediaTypeUnknown
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user