forked from mtusi2026/mtusi2026lab4
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log"
|
|
"mime"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service"
|
|
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
|
|
)
|
|
|
|
func createAttachment(w http.ResponseWriter, r *http.Request, attachments *service.Attachments, chatID string) {
|
|
if !requireMethod(w, r, http.MethodPost) {
|
|
return
|
|
}
|
|
if attachments == nil {
|
|
writeAppError(w, store.ErrUnavailable)
|
|
return
|
|
}
|
|
query, err := url.ParseQuery(r.URL.RawQuery)
|
|
if err != nil || len(query["filename"]) != 1 {
|
|
writeAppError(w, service.ErrInvalidRequest)
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 45*time.Second)
|
|
defer cancel()
|
|
a, err := attachments.Create(ctx, userFromContext(ctx).ID, chatID, query.Get("filename"), r.Header.Get("Content-Type"), r.Body, r.ContentLength)
|
|
if err != nil {
|
|
writeAppError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, a)
|
|
}
|
|
|
|
func attachmentRoutes(w http.ResponseWriter, r *http.Request, attachments *service.Attachments) {
|
|
if !requireMethod(w, r, http.MethodGet) {
|
|
return
|
|
}
|
|
if attachments == nil {
|
|
writeAppError(w, store.ErrUnavailable)
|
|
return
|
|
}
|
|
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/attachments/"), "/")
|
|
if parts[0] == "" || len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") {
|
|
writeAppError(w, store.ErrNotFound)
|
|
return
|
|
}
|
|
a, err := attachments.Get(r.Context(), userFromContext(r.Context()).ID, parts[0])
|
|
if err != nil {
|
|
writeAppError(w, err)
|
|
return
|
|
}
|
|
if len(parts) == 1 {
|
|
writeJSON(w, http.StatusOK, a)
|
|
return
|
|
}
|
|
query, err := url.ParseQuery(r.URL.RawQuery)
|
|
if err != nil {
|
|
writeAppError(w, service.ErrInvalidRequest)
|
|
return
|
|
}
|
|
if len(query["variant"]) > 1 {
|
|
writeAppError(w, service.ErrInvalidRequest)
|
|
return
|
|
}
|
|
if variant := query.Get("variant"); variant != "" && variant != "original" {
|
|
writeAppError(w, store.ErrNotFound)
|
|
return
|
|
}
|
|
original, err := attachments.Open(r.Context(), a)
|
|
if err != nil {
|
|
writeAppError(w, err)
|
|
return
|
|
}
|
|
defer original.Body.Close()
|
|
w.Header().Set("Content-Type", a.ContentType)
|
|
w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
|
|
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": a.Filename}))
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
if _, err := io.CopyBuffer(w, original.Body, make([]byte, 32*1024)); err != nil {
|
|
log.Print("attachment download stream interrupted")
|
|
// Headers may already be sent: abort rather than append a JSON error to bytes.
|
|
panic(http.ErrAbortHandler)
|
|
}
|
|
}
|