go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/casviewer/blobs.go (about) 1 // Copyright 2020 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package casviewer 16 17 import ( 18 "context" 19 "fmt" 20 "net/http" 21 22 "github.com/bazelbuild/remote-apis-sdks/go/pkg/client" 23 "github.com/bazelbuild/remote-apis-sdks/go/pkg/digest" 24 repb "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2" 25 "google.golang.org/grpc/status" 26 "google.golang.org/protobuf/proto" 27 28 "go.chromium.org/luci/common/errors" 29 "go.chromium.org/luci/grpc/grpcutil" 30 "go.chromium.org/luci/server/templates" 31 ) 32 33 // renderTree renders a Directory. 34 func renderTree(ctx context.Context, w http.ResponseWriter, cl *client.Client, bd *digest.Digest, instance string) error { 35 b, err := readBlob(ctx, cl, bd) 36 if err != nil { 37 return err 38 } 39 40 d := &repb.Directory{} 41 err = proto.Unmarshal(b, d) 42 if err != nil { 43 return errors.Annotate(err, "blob must be directory").Tag(grpcutil.InvalidArgumentTag).Err() 44 } 45 46 templates.MustRender(ctx, w, "pages/tree.html", templates.Args{ 47 "Instance": instance, 48 "Digest": bd.String(), 49 "Directories": d.GetDirectories(), 50 "Files": d.GetFiles(), 51 "Symlinks": d.GetSymlinks(), 52 }) 53 54 return nil 55 } 56 57 // returnBlob writes a blob to response. 58 func returnBlob(ctx context.Context, w http.ResponseWriter, cl *client.Client, bd *digest.Digest, filename string) error { 59 b, err := readBlob(ctx, cl, bd) 60 if err != nil { 61 return err 62 } 63 64 // Set "text/plan" for the browser to detect the file type and display it. 65 // - Images will be wrapped in <image>. 66 // - Texts will be wrapped in <pre>. 67 // - Videos/Audios will be wrapped in <video>. 68 // etc. 69 w.Header().Add("Content-Type", "text/plain; charset=utf-8") 70 71 // Set the filename for download. 72 if filename != "" { 73 w.Header().Add("Content-Disposition", fmt.Sprintf("filename=%s", filename)) 74 } 75 76 _, err = w.Write(b) 77 if err != nil { 78 return err 79 } 80 81 return nil 82 } 83 84 // readBlob reads the blob from CAS. 85 func readBlob(ctx context.Context, cl *client.Client, bd *digest.Digest) ([]byte, error) { 86 b, _, err := cl.ReadBlob(ctx, *bd) 87 if err != nil { 88 // convert gRPC code to LUCI errors tag. 89 t := grpcutil.Tag.With(status.Code(err)) 90 return nil, errors.Annotate(err, "failed to read blob").Tag(t).Err() 91 } 92 return b, nil 93 } 94 95 // treeURL renders a URL to the tree page. 96 func treeURL(instance string, d *repb.DirectoryNode) string { 97 return fmt.Sprintf("/%s/blobs/%s/%d/tree", instance, d.Digest.Hash, d.Digest.SizeBytes) 98 } 99 100 // getURL renders a URL to the tree page. 101 func getURL(instance string, f *repb.FileNode) string { 102 return fmt.Sprintf("/%s/blobs/%s/%d?filename=%s", instance, f.Digest.Hash, f.Digest.SizeBytes, f.Name) 103 }