From e5bbc585e339031fbfafe5c529b53d45d16ee2fd Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sat, 11 Sep 2021 16:05:31 +0300 Subject: [PATCH] feat: write files by id --- controllers/threads_controller.go | 35 +++++++++++++++++-------------- repositories/files_repository.go | 14 +++++++------ templates/index.html | 2 +- templates/thread.html | 2 +- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index a683d0c..be922e2 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -7,6 +7,7 @@ import ( "net/http" "path/filepath" "strconv" + "strings" "github.com/gin-gonic/gin" @@ -127,25 +128,26 @@ func CreateThread(c *gin.Context) { } for _, fileInRequest := range filesInRequest { - path := filepath.Join( - Utils.UPLOADS_DIR_PATH, - strconv.Itoa(postID), - fileInRequest.Filename, - ) file := Repositories.File{ PostID: postID, Name: fileInRequest.Filename, - Ext: fileInRequest.Header["Content-Type"][0], - Size: int(fileInRequest.Size), + // image/jpeg -> jpeg + Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1], + Size: int(fileInRequest.Size), } - err := Repositories.Files.CreateInTx(tx, file) + fileID, err := Repositories.Files.CreateInTx(tx, file) if err != nil { log.Println("error:", err) c.HTML(http.StatusInternalServerError, "500.html", nil) return } + path := filepath.Join( + Utils.UPLOADS_DIR_PATH, + strconv.Itoa(postID), + strconv.Itoa(fileID)+"."+file.Ext, + ) err = c.SaveUploadedFile(fileInRequest, path) if err != nil { log.Println("error:", err) @@ -217,25 +219,26 @@ func UpdateThread(c *gin.Context) { } for _, fileInRequest := range filesInRequest { - path := filepath.Join( - Utils.UPLOADS_DIR_PATH, - strconv.Itoa(threadID), - fileInRequest.Filename, - ) file := Repositories.File{ PostID: postID, Name: fileInRequest.Filename, - Ext: fileInRequest.Header["Content-Type"][0], - Size: int(fileInRequest.Size), + // image/jpeg -> jpeg + Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1], + Size: int(fileInRequest.Size), } - err := Repositories.Files.CreateInTx(tx, file) + fileID, err := Repositories.Files.CreateInTx(tx, file) if err != nil { log.Println("error:", err) c.HTML(http.StatusInternalServerError, "500.html", nil) return } + path := filepath.Join( + Utils.UPLOADS_DIR_PATH, + strconv.Itoa(threadID), + strconv.Itoa(fileID)+"."+file.Ext, + ) err = c.SaveUploadedFile(fileInRequest, path) if err != nil { log.Println("error:", err) diff --git a/repositories/files_repository.go b/repositories/files_repository.go index 3169408..dbf4c2e 100644 --- a/repositories/files_repository.go +++ b/repositories/files_repository.go @@ -32,7 +32,7 @@ func (r *FilesRepository) Create(f File) error { func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { sql := ` - SELECT post_id, name, size + SELECT id, post_id, name, size, ext FROM files WHERE post_id = ANY ($1) ORDER BY id ASC @@ -52,7 +52,7 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { filesMapByPostId := make(map[int][]File) for rows.Next() { var file File - err = rows.Scan(&file.PostID, &file.Name, &file.Size) + err = rows.Scan(&file.ID, &file.PostID, &file.Name, &file.Size, &file.Ext) if err != nil { return nil, err } @@ -70,20 +70,22 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { return filesMapByPostId, nil } -func (r *FilesRepository) CreateInTx(tx pgx.Tx, f File) error { +func (r *FilesRepository) CreateInTx(tx pgx.Tx, f File) (int, error) { sql := ` INSERT INTO files (post_id, name, ext, size) VALUES ($1, $2, $3, $4) + RETURNING id ` row := tx.QueryRow( context.TODO(), sql, f.PostID, f.Name, f.Ext, f.Size, ) - err := row.Scan() + createdFile := new(File) + err := row.Scan(&createdFile.ID) if err != nil && err != pgx.ErrNoRows { - return err + return 0, err } - return nil + return createdFile.ID, nil } diff --git a/templates/index.html b/templates/index.html index 9d2124d..e4d6427 100644 --- a/templates/index.html +++ b/templates/index.html @@ -24,7 +24,7 @@ {{ if gt $length 0 }} {{ $FirstFile := index $Post.Files 0 }} Uploaded picture diff --git a/templates/thread.html b/templates/thread.html index 175d13b..dd16646 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -20,7 +20,7 @@ {{ $length := len $Post.Files }} {{ if gt $length 0 }} {{$FirstFile := index $Post.Files 0}} Uploaded picture