feat: write files by id

This commit is contained in:
Yanislav Igonin 2021-09-11 16:05:31 +03:00
parent b54db6f729
commit e5bbc585e3
4 changed files with 29 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -127,25 +128,26 @@ func CreateThread(c *gin.Context) {
} }
for _, fileInRequest := range filesInRequest { for _, fileInRequest := range filesInRequest {
path := filepath.Join(
Utils.UPLOADS_DIR_PATH,
strconv.Itoa(postID),
fileInRequest.Filename,
)
file := Repositories.File{ file := Repositories.File{
PostID: postID, PostID: postID,
Name: fileInRequest.Filename, Name: fileInRequest.Filename,
Ext: fileInRequest.Header["Content-Type"][0], // image/jpeg -> jpeg
Size: int(fileInRequest.Size), 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 { if err != nil {
log.Println("error:", err) log.Println("error:", err)
c.HTML(http.StatusInternalServerError, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return return
} }
path := filepath.Join(
Utils.UPLOADS_DIR_PATH,
strconv.Itoa(postID),
strconv.Itoa(fileID)+"."+file.Ext,
)
err = c.SaveUploadedFile(fileInRequest, path) err = c.SaveUploadedFile(fileInRequest, path)
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
@ -217,25 +219,26 @@ func UpdateThread(c *gin.Context) {
} }
for _, fileInRequest := range filesInRequest { for _, fileInRequest := range filesInRequest {
path := filepath.Join(
Utils.UPLOADS_DIR_PATH,
strconv.Itoa(threadID),
fileInRequest.Filename,
)
file := Repositories.File{ file := Repositories.File{
PostID: postID, PostID: postID,
Name: fileInRequest.Filename, Name: fileInRequest.Filename,
Ext: fileInRequest.Header["Content-Type"][0], // image/jpeg -> jpeg
Size: int(fileInRequest.Size), 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 { if err != nil {
log.Println("error:", err) log.Println("error:", err)
c.HTML(http.StatusInternalServerError, "500.html", nil) c.HTML(http.StatusInternalServerError, "500.html", nil)
return return
} }
path := filepath.Join(
Utils.UPLOADS_DIR_PATH,
strconv.Itoa(threadID),
strconv.Itoa(fileID)+"."+file.Ext,
)
err = c.SaveUploadedFile(fileInRequest, path) err = c.SaveUploadedFile(fileInRequest, path)
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)

View File

@ -32,7 +32,7 @@ func (r *FilesRepository) Create(f File) error {
func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) { func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) {
sql := ` sql := `
SELECT post_id, name, size SELECT id, post_id, name, size, ext
FROM files FROM files
WHERE post_id = ANY ($1) WHERE post_id = ANY ($1)
ORDER BY id ASC ORDER BY id ASC
@ -52,7 +52,7 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) {
filesMapByPostId := make(map[int][]File) filesMapByPostId := make(map[int][]File)
for rows.Next() { for rows.Next() {
var file File 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 { if err != nil {
return nil, err return nil, err
} }
@ -70,20 +70,22 @@ func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) {
return filesMapByPostId, nil 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 := ` sql := `
INSERT INTO files (post_id, name, ext, size) INSERT INTO files (post_id, name, ext, size)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4)
RETURNING id
` `
row := tx.QueryRow( row := tx.QueryRow(
context.TODO(), sql, f.PostID, f.Name, f.Ext, f.Size, 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 { if err != nil && err != pgx.ErrNoRows {
return err return 0, err
} }
return nil return createdFile.ID, nil
} }

View File

@ -24,7 +24,7 @@
{{ if gt $length 0 }} {{ if gt $length 0 }}
{{ $FirstFile := index $Post.Files 0 }} {{ $FirstFile := index $Post.Files 0 }}
<img <img
src="/uploads/{{$Post.ID}}/{{$FirstFile.Name}}" src="/uploads/{{$Post.ID}}/{{$FirstFile.ID}}.{{$FirstFile.Ext}}"
class="card-img-top" class="card-img-top"
alt="Uploaded picture" alt="Uploaded picture"
> >

View File

@ -20,7 +20,7 @@
{{ $length := len $Post.Files }} {{ if gt $length 0 }} {{ $length := len $Post.Files }} {{ if gt $length 0 }}
{{$FirstFile := index $Post.Files 0}} {{$FirstFile := index $Post.Files 0}}
<img <img
src="/uploads/{{$FirstPost.ID}}/{{$FirstFile.Name}}" src="/uploads/{{$FirstPost.ID}}/{{$FirstFile.ID}}.{{$FirstFile.Ext}}"
class="card-img-top" class="card-img-top"
alt="Uploaded picture" alt="Uploaded picture"
> >