github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/screenshot_redirect_handler.go (about)

     1  // Copyright 2017 The WPT Dashboard Project. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/gorilla/mux"
    12  	"github.com/web-platform-tests/wpt.fyi/shared"
    13  )
    14  
    15  // apiScreenshotRedirectHandler is responsible for redirecting to the Google Cloud Storage API
    16  // png blob for the given screenshot hash.
    17  //
    18  // URL format:
    19  // "/api/screenshot/{screenshot}".
    20  func apiScreenshotRedirectHandler(w http.ResponseWriter, r *http.Request) {
    21  	shot := mux.Vars(r)["screenshot"]
    22  	if shot == "" {
    23  		http.Error(w, "Screenshot id missing", http.StatusBadRequest)
    24  	}
    25  
    26  	ctx := r.Context()
    27  	aeAPI := shared.NewAppEngineAPI(ctx)
    28  	/* #nosec G101 */
    29  	bucket := "wptd-screenshots-staging"
    30  	if aeAPI.GetHostname() == "wpt.fyi" {
    31  		/* #nosec G101 */
    32  		bucket = "wptd-screenshots"
    33  	}
    34  	url := fmt.Sprintf("https://storage.googleapis.com/%s/%s.png", bucket, shot)
    35  	http.Redirect(w, r, url, http.StatusPermanentRedirect)
    36  }