Home Download Docs Code Community
     1	/*
     2	Copyright 2011 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	package server
    18	
    19	import (
    20		"log"
    21		"net/http"
    22	
    23		"perkeep.org/internal/httputil"
    24		"perkeep.org/pkg/blob"
    25		"perkeep.org/pkg/schema"
    26	)
    27	
    28	type FileTreeHandler struct {
    29		Fetcher blob.Fetcher
    30		file    blob.Ref
    31	}
    32	
    33	// FileTreeNode represents a file in a file tree.
    34	// It is part of the FileTreeResponse.
    35	type FileTreeNode struct {
    36		// Name is the basename of the node.
    37		Name string `json:"name"`
    38		// Type is the camliType of the node. This may be "file", "directory", "symlink"
    39		// or other in the future.
    40		Type schema.CamliType `json:"type"`
    41		// BlobRef is the blob.Ref of the node.
    42		BlobRef blob.Ref `json:"blobRef"`
    43	}
    44	
    45	// FileTreeResponse is the JSON response for the FileTreeHandler.
    46	type FileTreeResponse struct {
    47		// Children is the list of children files of a directory.
    48		Children []FileTreeNode `json:"children"`
    49	}
    50	
    51	func (fth *FileTreeHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    52		ctx := req.Context()
    53		if req.Method != "GET" && req.Method != "HEAD" {
    54			http.Error(rw, "Invalid method", http.StatusBadRequest)
    55			return
    56		}
    57	
    58		de, err := schema.NewDirectoryEntryFromBlobRef(ctx, fth.Fetcher, fth.file)
    59		if err != nil {
    60			http.Error(rw, "Error reading directory", 500)
    61			log.Printf("Error reading directory from blobref %s: %v\n", fth.file, err)
    62			return
    63		}
    64		dir, err := de.Directory(ctx)
    65		if err != nil {
    66			http.Error(rw, "Error reading directory", 500)
    67			log.Printf("Error reading directory from blobref %s: %v\n", fth.file, err)
    68			return
    69		}
    70		entries, err := dir.Readdir(ctx, -1)
    71		if err != nil {
    72			http.Error(rw, "Error reading directory", 500)
    73			log.Printf("reading dir from blobref %s: %v\n", fth.file, err)
    74			return
    75		}
    76	
    77		var ret = FileTreeResponse{
    78			Children: make([]FileTreeNode, 0, len(entries)),
    79		}
    80		for _, v := range entries {
    81			ret.Children = append(ret.Children, FileTreeNode{
    82				Name:    v.FileName(),
    83				Type:    v.CamliType(),
    84				BlobRef: v.BlobRef(),
    85			})
    86		}
    87		httputil.ReturnJSON(rw, ret)
    88	}
Website layout inspired by memcached.
Content by the authors.