// GetDownloadURL is a handler for the /assets GET API endpoint. It uses dependency injection to allow for more robust testing.
GetDownloadURL is a handler for the /assets GET API endpoint. It uses dependency injection to allow for more robust testing.
// GetDownloadURL is a handler for the / assets GET API endpoint . It uses dependency injection to allow for more robust testing .
func GetDownloadURL(env *config.Env) http.HandlerFunc {
	return func(wr http.ResponseWriter, r *http.Request) {
		wr.Header().Set(constants.HeaderContentType, constants.ApplicationJSON)
		id := r.URL.Query().Get("id")
		timeout := r.URL.Query().Get("timeout")
		asset, err := env.Store.GetAsset(id)
		if err != nil {
			switch err.(type) {
			case *data.ErrorNoAssetFound:
				responses.WriteResourceNotFoundResponse(wr, err.Error())
			default:
				responses.WriteInternalServerErrorResponse(wr, constants.InternalServerErrorMessage)
			}
			return
		}
		if asset.UploadStatus == constants.AssetStatusCreated {
			responses.WriteBadRequest(wr, constants.UnsetStatusMessage)
			return
		}
		url, err := env.AssetUploader.GetSignedDownloadURL(asset.Name, timeout)
		if err != nil {
			responses.WriteInternalServerErrorResponse(wr, constants.InternalServerErrorMessage)
			return
		}
		resp := &responses.DownloadURLResponse{
			ID:          id,
			DownloadURL: url,
		}
		responses.WriteOkResponse(wr, resp)
	}
}
func GetDownloadURL ( env * config . Env ) http . HandlerFunc { return func ( wr http . ResponseWriter , r * http . Request ) { wr . Header ( ) . Set ( constants . HeaderContentType , constants . ApplicationJSON ) id := r . URL . Query ( ) . Get ( " " ) timeout := r . URL . Query ( ) . Get ( " " ) asset , err := env . Store . GetAsset ( id ) if err != nil { switch err . ( type ) { case * data . ErrorNoAssetFound : responses . WriteResourceNotFoundResponse ( wr , err . Error ( ) ) default : responses . WriteInternalServerErrorResponse ( wr , constants . InternalServerErrorMessage ) } return } if asset . UploadStatus == constants . AssetStatusCreated { responses . WriteBadRequest ( wr , constants . UnsetStatusMessage ) return } url , err := env . AssetUploader . GetSignedDownloadURL ( asset . Name , timeout ) if err != nil { responses . WriteInternalServerErrorResponse ( wr , constants . InternalServerErrorMessage ) return } resp := & responses . DownloadURLResponse { ID : id , DownloadURL : url , } responses . WriteOkResponse ( wr , resp ) } }
GetDownloadURL is a handler for the /assets GET API endpoint.
GetDownloadURL is a handler for the / assets GET API endpoint .