mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: write files by id
This commit is contained in:
parent
b54db6f729
commit
e5bbc585e3
@ -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],
|
||||
// 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],
|
||||
// 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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
{{ if gt $length 0 }}
|
||||
{{ $FirstFile := index $Post.Files 0 }}
|
||||
<img
|
||||
src="/uploads/{{$Post.ID}}/{{$FirstFile.Name}}"
|
||||
src="/uploads/{{$Post.ID}}/{{$FirstFile.ID}}.{{$FirstFile.Ext}}"
|
||||
class="card-img-top"
|
||||
alt="Uploaded picture"
|
||||
>
|
||||
|
@ -20,7 +20,7 @@
|
||||
{{ $length := len $Post.Files }} {{ if gt $length 0 }}
|
||||
{{$FirstFile := index $Post.Files 0}}
|
||||
<img
|
||||
src="/uploads/{{$FirstPost.ID}}/{{$FirstFile.Name}}"
|
||||
src="/uploads/{{$FirstPost.ID}}/{{$FirstFile.ID}}.{{$FirstFile.Ext}}"
|
||||
class="card-img-top"
|
||||
alt="Uploaded picture"
|
||||
>
|
||||
|
Loading…
Reference in New Issue
Block a user