Browse Source

Processing handler: set do.CookieJar in makeDownloadOptions; Open downloading segment in fetchImage

DarthSim 3 months ago
parent
commit
a33adaaa14
2 changed files with 27 additions and 17 deletions
  1. 4 1
      handlers/processing/request.go
  2. 23 16
      handlers/processing/request_methods.go

+ 4 - 1
handlers/processing/request.go

@@ -62,7 +62,10 @@ func (r *request) execute(ctx context.Context) error {
 	imgRequestHeaders := r.makeImageRequestHeaders()
 
 	// create download options
-	do := r.makeDownloadOptions(ctx, imgRequestHeaders)
+	do, err := r.makeDownloadOptions(ctx, imgRequestHeaders)
+	if err != nil {
+		return err
+	}
 
 	// Fetch image actual
 	originData, originHeaders, err := r.fetchImage(ctx, do)

+ 23 - 16
handlers/processing/request_methods.go

@@ -56,27 +56,34 @@ func (r *request) acquireWorker(ctx context.Context) (context.CancelFunc, error)
 }
 
 // makeDownloadOptions creates a new default download options
-func (r *request) makeDownloadOptions(ctx context.Context, h http.Header) imagedata.DownloadOptions {
-	downloadFinished := r.Monitoring().StartDownloadingSegment(ctx, r.monitoringMeta.Filter(
-		monitoring.MetaSourceImageURL,
-		monitoring.MetaSourceImageOrigin,
-	))
+func (r *request) makeDownloadOptions(
+	ctx context.Context,
+	h http.Header,
+) (imagedata.DownloadOptions, error) {
+	jar, err := r.Cookies().JarFromRequest(r.req)
+	if err != nil {
+		return imagedata.DownloadOptions{}, ierrors.Wrap(
+			err, 0,
+			ierrors.WithCategory(handlers.CategoryDownload),
+		)
+	}
 
 	return imagedata.DownloadOptions{
-		Header:           h,
-		MaxSrcFileSize:   r.secops.MaxSrcFileSize,
-		DownloadFinished: downloadFinished,
-	}
+		Header:         h,
+		MaxSrcFileSize: r.secops.MaxSrcFileSize,
+		CookieJar:      jar,
+	}, nil
 }
 
 // fetchImage downloads the source image asynchronously
-func (r *request) fetchImage(ctx context.Context, do imagedata.DownloadOptions) (imagedata.ImageData, http.Header, error) {
-	var err error
-
-	do.CookieJar, err = r.Cookies().JarFromRequest(r.req)
-	if err != nil {
-		return nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryDownload))
-	}
+func (r *request) fetchImage(
+	ctx context.Context,
+	do imagedata.DownloadOptions,
+) (imagedata.ImageData, http.Header, error) {
+	do.DownloadFinished = r.Monitoring().StartDownloadingSegment(ctx, r.monitoringMeta.Filter(
+		monitoring.MetaSourceImageURL,
+		monitoring.MetaSourceImageOrigin,
+	))
 
 	return r.ImageDataFactory().DownloadAsync(ctx, r.imageURL, "source image", do)
 }