Skip to content

Commit

Permalink
Merge pull request #44 from go-park-mail-ru/BCFL-33-file-system
Browse files Browse the repository at this point in the history
Bcfl 33 file system
  • Loading branch information
Alexeyzem authored Dec 12, 2024
2 parents bd9e1d4 + 84e079b commit a5913d1
Show file tree
Hide file tree
Showing 9 changed files with 1,359 additions and 107 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ services:
dockerfile: Dockerfilefile
volumes:
- ./image:/image
- ./files:/files
restart: always
ports:
- "8083:8083"
Expand Down
81 changes: 62 additions & 19 deletions internal/fileService/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"mime/multipart"
"net/http"
"strings"

"github.com/gorilla/mux"

Expand All @@ -14,16 +15,19 @@ import (
)

var fileFormat = map[string]struct{}{
"image/jpeg": {},
"image/jpg": {},
"image/png": {},
"image/webp": {},
"jpeg": {},
"jpg": {},
"png": {},
"webp": {},
"gif": {},
}

//go:generate mockgen -destination=mock.go -source=$GOFILE -package=${GOPACKAGE}
type fileService interface {
Upload(ctx context.Context, name string) ([]byte, error)
Download(ctx context.Context, file multipart.File) (string, error)
Download(ctx context.Context, file multipart.File, format string) (string, error)
DownloadNonImage(ctx context.Context, file multipart.File, format string) (string, error)
UploadNonImage(ctx context.Context, name string) ([]byte, error)
}

type responder interface {
Expand All @@ -46,6 +50,31 @@ func NewFileController(fileService fileService, responder responder) *FileContro
}
}

func (fc *FileController) UploadNonImage(w http.ResponseWriter, r *http.Request) {
var (
reqID, ok = r.Context().Value("requestID").(string)
vars = mux.Vars(r)
name = vars["name"]
)

if !ok {
fc.responder.LogError(my_err.ErrInvalidContext, "")
}

if name == "" {
fc.responder.ErrorBadRequest(w, errors.New("name is empty"), reqID)
return
}

res, err := fc.fileService.UploadNonImage(r.Context(), name)
if err != nil {
fc.responder.ErrorBadRequest(w, fmt.Errorf("%w: %w", err, my_err.ErrWrongFile), reqID)
return
}

fc.responder.OutputBytes(w, res, reqID)
}

func (fc *FileController) Upload(w http.ResponseWriter, r *http.Request) {
var (
reqID, ok = r.Context().Value(middleware.RequestKey).(string)
Expand Down Expand Up @@ -77,35 +106,49 @@ func (fc *FileController) Download(w http.ResponseWriter, r *http.Request) {
fc.responder.LogError(my_err.ErrInvalidContext, "")
}

err := r.ParseMultipartForm(10 << 20) // 10Mbyte
err := r.ParseMultipartForm(10 * (10 << 20)) // 100Mbyte
if err != nil {
fc.responder.ErrorBadRequest(w, my_err.ErrToLargeFile, reqID)
return
}
defer func() {
err = r.MultipartForm.RemoveAll()
if err != nil {
panic(err)
fc.responder.LogError(err, reqID)
}
}()

file, header, err := r.FormFile("file")
if err != nil {
fc.responder.ErrorBadRequest(w, my_err.ErrToLargeFile, reqID)
fc.responder.ErrorBadRequest(w, err, reqID)
return
}

file, header, err := r.FormFile("file")
if err != nil {
file = nil
} else {
format := header.Header.Get("Content-Type")
if _, ok := fileFormat[format]; !ok {
fc.responder.ErrorBadRequest(w, my_err.ErrWrongFiletype, reqID)
return
defer func(file multipart.File) {
err = file.Close()
if err != nil {
fc.responder.LogError(err, reqID)
}
}(file)

formats := strings.Split(header.Header.Get("Content-Type"), "/")
if len(formats) != 2 {
fc.responder.ErrorBadRequest(w, my_err.ErrWrongFiletype, reqID)
return
}
var url string
format := formats[1]

if _, ok := fileFormat[format]; ok {
url, err = fc.fileService.Download(r.Context(), file, format)
} else {

url, err = fc.fileService.DownloadNonImage(r.Context(), file, format)
}
defer file.Close()

url, err := fc.fileService.Download(r.Context(), file)
if err != nil {
fc.responder.ErrorBadRequest(w, err, reqID)
return
}

fc.responder.OutputJSON(w, url, reqID)
}
Loading

0 comments on commit a5913d1

Please sign in to comment.