Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ipopova committed Apr 30, 2024
1 parent 8cb5258 commit 6e129dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
14 changes: 7 additions & 7 deletions options/processing_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ func parsePathOptions(parts []string, headers http.Header) (*ProcessingOptions,
url, extension, err := DecodeURL(urlParts)

if err != nil {
return nil, url, err
return nil, "", err
}

if err = applyURLOptions(po, options); err != nil {
Expand All @@ -1162,7 +1162,7 @@ func parsePathOptions(parts []string, headers http.Header) (*ProcessingOptions,

if !po.Raw && len(extension) > 0 {
if err = applyFormatOption(po, []string{extension}); err != nil {
return nil, "", err
return nil, url, err
}
}

Expand All @@ -1178,18 +1178,18 @@ func parsePathPresets(parts []string, headers http.Header) (*ProcessingOptions,
presets := strings.Split(parts[0], ":")
urlParts := parts[1:]

if err = applyPresetOption(po, presets); err != nil {
return po, "", err
}

url, extension, err := DecodeURL(urlParts)
if err != nil {
return nil, "", err
}

if err = applyPresetOption(po, presets); err != nil {
return po, url, err
}

if !po.Raw && len(extension) > 0 {
if err = applyFormatOption(po, []string{extension}); err != nil {
return nil, "", err
return nil, url, err
}
}

Expand Down
31 changes: 15 additions & 16 deletions processing_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
defer token.Release()
}

signature, path, err := getPathAndSignature(r.RequestURI)
cleanedPath := cleanPath(r.URL.Path)

signature, path, err := trimSignature(cleanedPath)

checkErr(ctx, "path_parsing", err)

Expand Down Expand Up @@ -321,6 +323,7 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {

ierr := ierrors.Wrap(err, 0)
ierr.Unexpected = ierr.Unexpected || config.ReportDownloadingErrors
ierr.SourceImage = imageURL

sendErr(ctx, "download", ierr)

Expand Down Expand Up @@ -419,7 +422,11 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
return processing.ProcessImage(ctx, originData, po)
}()

checkErr(ctx, "processing", err)
if err != nil {
sendErrAndPanic(ctx, "processing", ierrors.New(
422, err.Error(), "Cannot process image",
).WithSourceImageField(imageURL))
}

defer resultData.Close()

Expand All @@ -428,21 +435,13 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
respondWithImage(reqID, r, rw, statusCode, resultData, po, imageURL, originData)
}

func getPathAndSignature(path string) (string, string, error) {
result := ""

if queryStart := strings.IndexByte(path, '?'); queryStart >= 0 {
result = path[:queryStart]
}

if len(config.PathPrefix) > 0 {
result = strings.TrimPrefix(path, config.PathPrefix)
}

result = strings.TrimPrefix(path, "/")
func cleanPath(path string) string {
return strings.TrimPrefix(path, config.PathPrefix+"/")
}

if signatureEnd := strings.IndexByte(result, '/'); signatureEnd > 0 {
return result[:signatureEnd], result[signatureEnd:], nil
func trimSignature(path string) (string, string, error) {
if signatureEnd := strings.IndexByte(path, '/'); signatureEnd > 0 {
return path[:signatureEnd], path[signatureEnd:], nil
}

return "", "", ierrors.New(
Expand Down

0 comments on commit 6e129dd

Please sign in to comment.