2013-07-01 15:27:43 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors.
|
2013-07-01 15:27:43 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
package main
|
2013-07-01 15:27:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2013-07-01 15:27:43 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/schema"
|
|
|
|
"perkeep.org/pkg/search"
|
|
|
|
"perkeep.org/pkg/types/camtypes"
|
2013-07-01 15:27:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type zipHandler struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher blob.Fetcher
|
2014-06-14 20:14:34 +00:00
|
|
|
cl client // Used for search and describe requests.
|
|
|
|
// root is the "parent" permanode of everything to zip.
|
2013-07-01 15:27:43 +00:00
|
|
|
// Either a directory permanode, or a permanode with members.
|
2013-08-04 02:54:30 +00:00
|
|
|
root blob.Ref
|
2013-07-01 15:27:43 +00:00
|
|
|
// Optional name to use in the response header
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
// blobFile contains all the information we need about
|
|
|
|
// a file blob to add the corresponding file to a zip.
|
|
|
|
type blobFile struct {
|
2013-08-04 02:54:30 +00:00
|
|
|
blobRef blob.Ref
|
2013-07-01 15:27:43 +00:00
|
|
|
// path is the full path of the file from the root of the zip.
|
|
|
|
// slashes are always forward slashes, per the zip spec.
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type sortedFiles []*blobFile
|
|
|
|
|
|
|
|
func (s sortedFiles) Less(i, j int) bool { return s[i].path < s[j].path }
|
|
|
|
func (s sortedFiles) Len() int { return len(s) }
|
|
|
|
func (s sortedFiles) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
func (zh *zipHandler) describeMembers(br blob.Ref) (*search.DescribeResponse, error) {
|
2018-01-16 23:03:16 +00:00
|
|
|
res, err := zh.cl.Query(context.TODO(), &search.SearchQuery{
|
2014-06-14 20:14:34 +00:00
|
|
|
Constraint: &search.Constraint{
|
|
|
|
BlobRefPrefix: br.String(),
|
|
|
|
CamliType: "permanode",
|
|
|
|
},
|
|
|
|
Describe: &search.DescribeRequest{
|
2015-06-16 16:14:08 +00:00
|
|
|
Depth: 1,
|
2014-06-14 20:14:34 +00:00
|
|
|
Rules: []*search.DescribeRule{
|
|
|
|
{
|
|
|
|
Attrs: []string{"camliContent", "camliContentImage", "camliMember"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Limit: -1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not describe %v: %v", br, err)
|
|
|
|
}
|
|
|
|
if res == nil || res.Describe == nil {
|
|
|
|
return nil, fmt.Errorf("no describe result for %v", br)
|
|
|
|
}
|
|
|
|
return res.Describe, nil
|
|
|
|
}
|
|
|
|
|
2013-07-01 15:27:43 +00:00
|
|
|
// blobList returns the list of file blobs "under" dirBlob.
|
|
|
|
// It traverses permanode directories and permanode with members (collections).
|
2013-08-04 02:54:30 +00:00
|
|
|
func (zh *zipHandler) blobList(dirPath string, dirBlob blob.Ref) ([]*blobFile, error) {
|
2014-06-14 20:14:34 +00:00
|
|
|
// dr := zh.search.NewDescribeRequest()
|
|
|
|
// dr.Describe(dirBlob, 3)
|
|
|
|
// res, err := dr.Result()
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("Could not describe %v: %v", dirBlob, err)
|
|
|
|
// }
|
|
|
|
res, err := zh.describeMembers(dirBlob)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
2014-06-14 20:14:34 +00:00
|
|
|
return nil, err
|
2013-07-01 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
described := res.Meta[dirBlob.String()]
|
2013-07-01 15:27:43 +00:00
|
|
|
members := described.Members()
|
|
|
|
dirBlobPath, _, isDir := described.PermanodeDir()
|
|
|
|
if len(members) == 0 && !isDir {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var list []*blobFile
|
|
|
|
if isDir {
|
|
|
|
dirRoot := dirBlobPath[1]
|
2017-04-24 09:53:26 +00:00
|
|
|
children, err := zh.blobsFromDir("", dirRoot)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not get list of blobs from %v: %v", dirRoot, err)
|
|
|
|
}
|
|
|
|
list = append(list, children...)
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
for _, member := range members {
|
2014-06-14 20:14:34 +00:00
|
|
|
if fileBlobPath, fileInfo, ok := getFileInfo(member.BlobRef, res.Meta); ok {
|
2013-07-01 15:27:43 +00:00
|
|
|
// file
|
|
|
|
list = append(list,
|
|
|
|
&blobFile{fileBlobPath[1], path.Join(dirPath, fileInfo.FileName)})
|
|
|
|
continue
|
|
|
|
}
|
2014-06-14 20:14:34 +00:00
|
|
|
if dirBlobPath, dirInfo, ok := getDirInfo(member.BlobRef, res.Meta); ok {
|
2013-07-01 15:27:43 +00:00
|
|
|
// directory
|
|
|
|
newZipRoot := dirBlobPath[1]
|
|
|
|
children, err := zh.blobsFromDir(
|
|
|
|
path.Join(dirPath, dirInfo.FileName), newZipRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not get list of blobs from %v: %v", newZipRoot, err)
|
|
|
|
}
|
|
|
|
list = append(list, children...)
|
|
|
|
// TODO(mpl): we assume a directory permanode does not also have members.
|
|
|
|
// I know there is nothing preventing it, but does it make any sense?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// it might have members, so recurse
|
|
|
|
// If it does have members, we must consider it as a pseudo dir,
|
|
|
|
// so we can build a fullpath for each of its members.
|
|
|
|
// As a dir name, we're using its title if it has one, its (shortened)
|
|
|
|
// blobref otherwise.
|
|
|
|
pseudoDirName := member.Title()
|
|
|
|
if pseudoDirName == "" {
|
|
|
|
pseudoDirName = member.BlobRef.DigestPrefix(10)
|
|
|
|
}
|
|
|
|
fullpath := path.Join(dirPath, pseudoDirName)
|
|
|
|
moreMembers, err := zh.blobList(fullpath, member.BlobRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not get list of blobs from %v: %v", member.BlobRef, err)
|
|
|
|
}
|
|
|
|
list = append(list, moreMembers...)
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// blobsFromDir returns the list of file blobs in directory dirBlob.
|
|
|
|
// It only traverses permanode directories.
|
2013-08-04 02:54:30 +00:00
|
|
|
func (zh *zipHandler) blobsFromDir(dirPath string, dirBlob blob.Ref) ([]*blobFile, error) {
|
2013-07-01 15:27:43 +00:00
|
|
|
var list []*blobFile
|
2018-01-16 23:03:16 +00:00
|
|
|
dr, err := schema.NewDirReader(context.TODO(), zh.fetcher, dirBlob)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not read dir blob %v: %v", dirBlob, err)
|
|
|
|
}
|
2018-01-16 23:03:16 +00:00
|
|
|
ent, err := dr.Readdir(context.TODO(), -1)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not read dir entries: %v", err)
|
|
|
|
}
|
|
|
|
for _, v := range ent {
|
|
|
|
fullpath := path.Join(dirPath, v.FileName())
|
|
|
|
switch v.CamliType() {
|
2021-01-17 03:05:35 +00:00
|
|
|
case schema.TypeFile:
|
2013-07-01 15:27:43 +00:00
|
|
|
list = append(list, &blobFile{v.BlobRef(), fullpath})
|
2021-01-17 03:05:35 +00:00
|
|
|
case schema.TypeDirectory:
|
2013-07-01 15:27:43 +00:00
|
|
|
children, err := zh.blobsFromDir(fullpath, v.BlobRef())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not get list of blobs from %v: %v", v.BlobRef(), err)
|
|
|
|
}
|
|
|
|
list = append(list, children...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// renameDuplicates goes through bf to check for duplicate filepaths.
|
|
|
|
// It renames duplicate filepaths and returns a new slice, sorted by
|
|
|
|
// file path.
|
|
|
|
func renameDuplicates(bf []*blobFile) sortedFiles {
|
2013-08-04 02:54:30 +00:00
|
|
|
noDup := make(map[string]blob.Ref)
|
2013-07-01 15:27:43 +00:00
|
|
|
// use a map to detect duplicates and rename them
|
|
|
|
for _, file := range bf {
|
|
|
|
if _, ok := noDup[file.path]; ok {
|
|
|
|
// path already exists, so rename
|
|
|
|
suffix := 0
|
|
|
|
var newname string
|
|
|
|
for {
|
|
|
|
suffix++
|
|
|
|
ext := path.Ext(file.path)
|
|
|
|
newname = fmt.Sprintf("%s(%d)%s",
|
|
|
|
file.path[:len(file.path)-len(ext)], suffix, ext)
|
|
|
|
if _, ok := noDup[newname]; !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
noDup[newname] = file.blobRef
|
|
|
|
} else {
|
|
|
|
noDup[file.path] = file.blobRef
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reinsert in a slice and sort it
|
|
|
|
var sorted sortedFiles
|
|
|
|
for p, b := range noDup {
|
|
|
|
sorted = append(sorted, &blobFile{path: p, blobRef: b})
|
|
|
|
}
|
|
|
|
sort.Sort(sorted)
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP streams a zip archive of all the files "under"
|
|
|
|
// zh.root. That is, all the files pointed by file permanodes,
|
|
|
|
// which are directly members of zh.root or recursively down
|
|
|
|
// directory permanodes and permanodes members.
|
|
|
|
// To build the fullpath of a file in a collection, it uses
|
|
|
|
// the collection title if present, its blobRef otherwise, as
|
|
|
|
// a directory name.
|
|
|
|
func (zh *zipHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
// TODO: use http.ServeContent, so Range requests work and downloads can be resumed.
|
|
|
|
// Will require calculating the zip length once first (ideally as cheaply as possible,
|
|
|
|
// with dummy counting writer and dummy all-zero-byte-files of a fixed size),
|
|
|
|
// and then making a dummy ReadSeeker for ServeContent that can seek to the end,
|
|
|
|
// and then seek back to the beginning, but then seeks forward make it remember
|
|
|
|
// to skip that many bytes from the archive/zip writer when answering Reads.
|
2013-09-15 19:12:26 +00:00
|
|
|
if !httputil.IsGet(req) {
|
2013-07-01 15:27:43 +00:00
|
|
|
http.Error(rw, "Invalid method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2017-04-24 09:53:26 +00:00
|
|
|
bf, err := zh.blobList("", zh.root)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not serve zip for %v: %v", zh.root, err)
|
|
|
|
http.Error(rw, "Server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
blobFiles := renameDuplicates(bf)
|
|
|
|
|
|
|
|
h := rw.Header()
|
|
|
|
h.Set("Content-Type", "application/zip")
|
|
|
|
filename := zh.filename
|
|
|
|
if filename == "" {
|
|
|
|
filename = "download.zip"
|
|
|
|
}
|
|
|
|
h.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
|
|
|
|
zw := zip.NewWriter(rw)
|
|
|
|
etag := sha1.New()
|
|
|
|
for _, file := range blobFiles {
|
|
|
|
etag.Write([]byte(file.blobRef.String()))
|
|
|
|
}
|
|
|
|
h.Set("Etag", fmt.Sprintf(`"%x"`, etag.Sum(nil)))
|
|
|
|
|
|
|
|
for _, file := range blobFiles {
|
2018-01-16 23:03:16 +00:00
|
|
|
fr, err := schema.NewFileReader(context.TODO(), zh.fetcher, file.blobRef)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can not add %v in zip, not a file: %v", file.blobRef, err)
|
|
|
|
http.Error(rw, "Server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-04-26 23:02:35 +00:00
|
|
|
zh := zip.FileHeader{
|
2024-01-14 00:43:50 +00:00
|
|
|
Name: file.path,
|
|
|
|
Method: zip.Store,
|
|
|
|
Modified: fr.ModTime().UTC(),
|
2017-04-26 23:02:35 +00:00
|
|
|
}
|
|
|
|
f, err := zw.CreateHeader(&zh)
|
2013-07-01 15:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not create %q in zip: %v", file.path, err)
|
|
|
|
http.Error(rw, "Server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, fr)
|
|
|
|
fr.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not zip %q: %v", file.path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = zw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not close zipwriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 20:14:34 +00:00
|
|
|
|
|
|
|
// TODO(mpl): refactor with getFileInfo
|
|
|
|
func getDirInfo(item blob.Ref, peers map[string]*search.DescribedBlob) (path []blob.Ref, di *camtypes.FileInfo, ok bool) {
|
|
|
|
described := peers[item.String()]
|
|
|
|
if described == nil ||
|
|
|
|
described.Permanode == nil ||
|
|
|
|
described.Permanode.Attr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contentRef := described.Permanode.Attr.Get("camliContent")
|
|
|
|
if contentRef == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cdes := peers[contentRef]; cdes != nil && cdes.Dir != nil {
|
|
|
|
return []blob.Ref{described.BlobRef, cdes.BlobRef}, cdes.Dir, true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|