Skip to content

Commit

Permalink
refactor: Reducing cyclomatic complexity for new chunk part
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jul 16, 2022
1 parent 527be14 commit 49e7d30
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ ENTRYPOINT [ "/fibr" ]
ARG VERSION
ENV VERSION ${VERSION}

VOLUME /tmp

ARG TARGETOS
ARG TARGETARCH

COPY passwd /etc/passwd
USER 995

VOLUME /tmp

COPY mime.types /etc/mime.types
COPY ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY release/fibr_${TARGETOS}_${TARGETARCH} /fibr
3 changes: 1 addition & 2 deletions pkg/crud/browser.go → pkg/crud/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/renderer"
)

// Browser render file web view
func (a App) Browser(ctx context.Context, w http.ResponseWriter, request provider.Request, item absto.Item, message renderer.Message) (renderer.Page, error) {
func (a App) browse(ctx context.Context, w http.ResponseWriter, request provider.Request, item absto.Item, message renderer.Message) (renderer.Page, error) {
var (
previous provider.RenderItem
next provider.RenderItem
Expand Down
6 changes: 3 additions & 3 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (a App) handleFile(w http.ResponseWriter, r *http.Request, request provider

go a.notify(provider.NewAccessEvent(item, r))

return a.Browser(r.Context(), w, request, item, message)
return a.browse(r.Context(), w, request, item, message)
}

return renderer.Page{}, a.serveFile(w, r, item)
Expand All @@ -100,7 +100,7 @@ func (a App) serveFile(w http.ResponseWriter, r *http.Request, item absto.Item)

func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.Request, item absto.Item, message renderer.Message) (renderer.Page, error) {
if query.GetBool(r, "stats") {
return a.Stats(w, r, request, message)
return a.stats(w, r, request, message)
}

items, err := a.listFiles(r, request, item)
Expand Down Expand Up @@ -135,7 +135,7 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
return a.story(r, request, item, items)
}

return a.List(r.Context(), request, message, item, items)
return a.list(r.Context(), request, message, item, items)
}

func (a App) listFiles(r *http.Request, request provider.Request, item absto.Item) (items []absto.Item, err error) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func (a App) getCover(ctx context.Context, request provider.Request, files []abs
return nil
}

// List render directory web view of given dirPath
func (a App) List(ctx context.Context, request provider.Request, message renderer.Message, item absto.Item, files []absto.Item) (renderer.Page, error) {
func (a App) list(ctx context.Context, request provider.Request, message renderer.Message, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx, end := tracer.StartSpan(ctx, a.tracer, "list")
defer end()

Expand Down
8 changes: 4 additions & 4 deletions pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func (a App) handleMultipart(w http.ResponseWriter, r *http.Request, request pro

if len(r.Header.Get("X-Chunk-Upload")) != 0 {
if chunkNumber := r.Header.Get("X-Chunk-Number"); len(chunkNumber) != 0 {
a.UploadChunk(w, r, request, values["filename"], chunkNumber, file)
a.uploadChunk(w, r, request, values["filename"], chunkNumber, file)
} else {
a.MergeChunk(w, r, request, values)
a.mergeChunk(w, r, request, values)
}
} else {
a.Upload(w, r, request, values, file)
a.upload(w, r, request, values, file)
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ func (a App) handlePost(w http.ResponseWriter, r *http.Request, request provider
case http.MethodDelete:
a.Delete(w, r, request)
case http.MethodTrace:
a.Regenerate(w, r, request)
a.regenerate(w, r, request)
default:
a.error(w, r, request, model.WrapMethodNotAllowed(fmt.Errorf("unknown method `%s` for %s", method, r.URL.Path)))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/regenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/renderer"
)

// Regenerate regenerate start of the folder
func (a App) Regenerate(w http.ResponseWriter, r *http.Request, request provider.Request) {
func (a App) regenerate(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanEdit {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type entry struct {
Key string
}

// Stats render stats of the current
func (a App) Stats(w http.ResponseWriter, r *http.Request, request provider.Request, message renderer.Message) (renderer.Page, error) {
func (a App) stats(w http.ResponseWriter, r *http.Request, request provider.Request, message renderer.Message) (renderer.Page, error) {
pathname := request.Filepath()

stats, err := a.computeStats(r.Context(), pathname)
Expand Down
7 changes: 4 additions & 3 deletions pkg/crud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

func (a App) saveUploadedFile(ctx context.Context, request provider.Request, inputName, rawSize string, file *multipart.Part) (fileName string, err error) {
Expand Down Expand Up @@ -76,14 +77,14 @@ func getUploadSize(rawSize string) (int64, error) {
return size, nil
}

// Upload saves form files to filesystem
func (a App) Upload(w http.ResponseWriter, r *http.Request, request provider.Request, values map[string]string, file *multipart.Part) {
func (a App) upload(w http.ResponseWriter, r *http.Request, request provider.Request, values map[string]string, file *multipart.Part) {
if file == nil {
a.error(w, r, request, model.WrapInvalid(errors.New("no file provided for save")))
return
}

ctx := r.Context()
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "upload")
defer end()

filename, err := a.saveUploadedFile(ctx, request, values["filename"], values["size"], file)
if err != nil {
Expand Down
28 changes: 17 additions & 11 deletions pkg/crud/chunk.go → pkg/crud/upload_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

// UploadChunk save chunk file to a temp file
func (a App) UploadChunk(w http.ResponseWriter, r *http.Request, request provider.Request, fileName, chunkNumber string, file io.Reader) {
func (a App) uploadChunk(w http.ResponseWriter, r *http.Request, request provider.Request, fileName, chunkNumber string, file io.Reader) {
if file == nil {
a.error(w, r, request, model.WrapInvalid(errors.New("no file provided for save")))
return
Expand Down Expand Up @@ -61,8 +61,10 @@ func (a App) UploadChunk(w http.ResponseWriter, r *http.Request, request provide
w.WriteHeader(http.StatusAccepted)
}

// MergeChunk merges previously uploaded chunks into one file and move it to final destination
func (a App) MergeChunk(w http.ResponseWriter, r *http.Request, request provider.Request, values map[string]string) {
func (a App) mergeChunk(w http.ResponseWriter, r *http.Request, request provider.Request, values map[string]string) {
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "mergeChunk")
defer end()

var err error

fileName := values["filename"]
Expand Down Expand Up @@ -104,7 +106,7 @@ func (a App) MergeChunk(w http.ResponseWriter, r *http.Request, request provider
logger.Error("unable to delete chunk folder `%s`: %s", tempFolder, err)
}

a.postUpload(r.Context(), w, r, request, fileName, values)
a.postUpload(ctx, w, r, request, fileName, values)
}

func (a App) mergeChunkFiles(directory, destination string) error {
Expand All @@ -127,7 +129,15 @@ func (a App) mergeChunkFiles(directory, destination string) error {
}
}()

if err = filepath.WalkDir(directory, func(path string, info fs.DirEntry, err error) error {
if err = browseChunkFiles(directory, destination, writer); err != nil {
return fmt.Errorf("unable to walk chunks in `%s`: %s", directory, err)
}

return nil
}

func browseChunkFiles(directory, destination string, writer io.Writer) error {
return filepath.WalkDir(directory, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -152,9 +162,5 @@ func (a App) mergeChunkFiles(directory, destination string) error {
}

return nil
}); err != nil {
return fmt.Errorf("unable to walk chunks in `%s`: %s", directory, err)
}

return nil
})
}
5 changes: 0 additions & 5 deletions pkg/provider/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"mime/multipart"
"net/http"
"time"

Expand All @@ -18,13 +17,9 @@ import (
// Crud for user to interfact with filesystem
//go:generate mockgen -destination ../mocks/crud.go -mock_names Crud=Crud -package mocks github.com/ViBiOh/fibr/pkg/provider Crud
type Crud interface {
Start(done <-chan struct{})
Browser(context.Context, http.ResponseWriter, Request, absto.Item, renderer.Message) (renderer.Page, error)
List(context.Context, Request, renderer.Message, absto.Item, []absto.Item) (renderer.Page, error)
Get(http.ResponseWriter, *http.Request, Request) (renderer.Page, error)
Post(http.ResponseWriter, *http.Request, Request)
Create(http.ResponseWriter, *http.Request, Request)
Upload(http.ResponseWriter, *http.Request, Request, map[string]string, *multipart.Part)
Rename(http.ResponseWriter, *http.Request, Request)
Delete(http.ResponseWriter, *http.Request, Request)
}
Expand Down

0 comments on commit 49e7d30

Please sign in to comment.