|
| 1 | +package drives |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "files/pkg/common" |
| 6 | + "files/pkg/files" |
| 7 | + "files/pkg/fileutils" |
| 8 | + "github.com/mholt/archiver/v3" |
| 9 | + "k8s.io/klog/v2" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + gopath "path" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +func slashClean(name string) string { |
| 18 | + if name == "" || name[0] != '/' { |
| 19 | + name = "/" + name |
| 20 | + } |
| 21 | + return gopath.Clean(name) |
| 22 | +} |
| 23 | + |
| 24 | +func parseQueryFiles(r *http.Request, f *files.FileInfo) ([]string, error) { |
| 25 | + var fileSlice []string |
| 26 | + names := strings.Split(r.URL.Query().Get("files"), ",") |
| 27 | + |
| 28 | + if len(names) == 0 { |
| 29 | + fileSlice = append(fileSlice, f.Path) |
| 30 | + } else { |
| 31 | + for _, name := range names { |
| 32 | + name, err := url.QueryUnescape(strings.Replace(name, "+", "%2B", -1)) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + name = slashClean(name) |
| 38 | + fileSlice = append(fileSlice, filepath.Join(f.Path, name)) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return fileSlice, nil |
| 43 | +} |
| 44 | + |
| 45 | +func parseQueryAlgorithm(r *http.Request) (string, archiver.Writer, error) { |
| 46 | + switch r.URL.Query().Get("algo") { |
| 47 | + case "zip", "true", "": |
| 48 | + return ".zip", archiver.NewZip(), nil |
| 49 | + case "tar": |
| 50 | + return ".tar", archiver.NewTar(), nil |
| 51 | + case "targz": |
| 52 | + return ".tar.gz", archiver.NewTarGz(), nil |
| 53 | + case "tarbz2": |
| 54 | + return ".tar.bz2", archiver.NewTarBz2(), nil |
| 55 | + case "tarxz": |
| 56 | + return ".tar.xz", archiver.NewTarXz(), nil |
| 57 | + case "tarlz4": |
| 58 | + return ".tar.lz4", archiver.NewTarLz4(), nil |
| 59 | + case "tarsz": |
| 60 | + return ".tar.sz", archiver.NewTarSz(), nil |
| 61 | + default: |
| 62 | + return "", nil, errors.New("format not implemented") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func SetContentDisposition(w http.ResponseWriter, r *http.Request, file *files.FileInfo) { |
| 67 | + if r.URL.Query().Get("inline") == "true" { |
| 68 | + w.Header().Set("Content-Disposition", "inline") |
| 69 | + } else { |
| 70 | + // As per RFC6266 section 4.3 |
| 71 | + w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(file.Name)) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func AddFile(ar archiver.Writer, d *common.Data, path, commonPath string) error { |
| 76 | + info, err := files.DefaultFs.Stat(path) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + if !info.IsDir() && !info.Mode().IsRegular() { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + file, err := files.DefaultFs.Open(path) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + defer file.Close() |
| 90 | + |
| 91 | + if path != commonPath { |
| 92 | + filename := strings.TrimPrefix(path, commonPath) |
| 93 | + filename = strings.TrimPrefix(filename, string(filepath.Separator)) |
| 94 | + err = ar.Write(archiver.File{ |
| 95 | + FileInfo: archiver.FileInfo{ |
| 96 | + FileInfo: info, |
| 97 | + CustomName: filename, |
| 98 | + }, |
| 99 | + ReadCloser: file, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if info.IsDir() { |
| 107 | + names, err := file.Readdirnames(0) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + for _, name := range names { |
| 113 | + fPath := filepath.Join(path, name) |
| 114 | + err = AddFile(ar, d, fPath, commonPath) |
| 115 | + if err != nil { |
| 116 | + klog.Errorf("Failed to archive %s: %v", fPath, err) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func RawDirHandler(w http.ResponseWriter, r *http.Request, d *common.Data, file *files.FileInfo) (int, error) { |
| 125 | + filenames, err := parseQueryFiles(r, file) |
| 126 | + if err != nil { |
| 127 | + return http.StatusInternalServerError, err |
| 128 | + } |
| 129 | + |
| 130 | + extension, ar, err := parseQueryAlgorithm(r) |
| 131 | + if err != nil { |
| 132 | + return http.StatusInternalServerError, err |
| 133 | + } |
| 134 | + |
| 135 | + err = ar.Create(w) |
| 136 | + if err != nil { |
| 137 | + return http.StatusInternalServerError, err |
| 138 | + } |
| 139 | + defer ar.Close() |
| 140 | + |
| 141 | + commonDir := fileutils.CommonPrefix(filepath.Separator, filenames...) |
| 142 | + |
| 143 | + name := filepath.Base(commonDir) |
| 144 | + if name == "." || name == "" || name == string(filepath.Separator) { |
| 145 | + name = file.Name |
| 146 | + } |
| 147 | + // Prefix used to distinguish a filelist generated |
| 148 | + // archive from the full directory archive |
| 149 | + if len(filenames) > 1 { |
| 150 | + name = "_" + name |
| 151 | + } |
| 152 | + name += extension |
| 153 | + w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name)) |
| 154 | + |
| 155 | + for _, fname := range filenames { |
| 156 | + err = AddFile(ar, d, fname, commonDir) |
| 157 | + if err != nil { |
| 158 | + klog.Errorf("Failed to archive %s: %v", fname, err) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return 0, nil |
| 163 | +} |
| 164 | + |
| 165 | +func RawFileHandler(w http.ResponseWriter, r *http.Request, file *files.FileInfo) (int, error) { |
| 166 | + fd, err := file.Fs.Open(file.Path) |
| 167 | + if err != nil { |
| 168 | + return http.StatusInternalServerError, err |
| 169 | + } |
| 170 | + defer fd.Close() |
| 171 | + |
| 172 | + SetContentDisposition(w, r, file) |
| 173 | + |
| 174 | + w.Header().Set("Cache-Control", "private") |
| 175 | + http.ServeContent(w, r, file.Name, file.ModTime, fd) |
| 176 | + return 0, nil |
| 177 | +} |
0 commit comments