~hduran-8/+junk/caddy

« back to all changes in this revision

Viewing changes to debian/gocode/src/github.com/mholt/caddy/caddyhttp/browse/browse.go

  • Committer: Horacio Durán
  • Date: 2017-01-20 16:21:20 UTC
  • Revision ID: horacio.duran@canonical.com-20170120162120-l82mfqwmsclnk838
Upgrade to 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
        "github.com/mholt/caddy/caddyhttp/staticfiles"
21
21
)
22
22
 
 
23
const (
 
24
        sortByName = "name"
 
25
        sortBySize = "size"
 
26
        sortByTime = "time"
 
27
)
 
28
 
23
29
// Browse is an http.Handler that can show a file listing when
24
30
// directories in the given paths are specified.
25
31
type Browse struct {
31
37
// Config is a configuration for browsing in a particular path.
32
38
type Config struct {
33
39
        PathScope string
34
 
        Root      http.FileSystem
 
40
        Fs        staticfiles.FileServer
35
41
        Variables interface{}
36
42
        Template  *template.Template
37
43
}
101
107
 
102
108
// FileInfo is the info about a particular file or directory
103
109
type FileInfo struct {
104
 
        IsDir   bool
105
110
        Name    string
106
111
        Size    int64
107
112
        URL     string
108
113
        ModTime time.Time
109
114
        Mode    os.FileMode
 
115
        IsDir   bool
110
116
}
111
117
 
112
118
// HumanSize returns the size of the file as a human-readable string
161
167
        // Check '.Order' to know how to sort
162
168
        if l.Order == "desc" {
163
169
                switch l.Sort {
164
 
                case "name":
 
170
                case sortByName:
165
171
                        sort.Sort(sort.Reverse(byName(l)))
166
 
                case "size":
 
172
                case sortBySize:
167
173
                        sort.Sort(sort.Reverse(bySize(l)))
168
 
                case "time":
 
174
                case sortByTime:
169
175
                        sort.Sort(sort.Reverse(byTime(l)))
170
176
                default:
171
177
                        // If not one of the above, do nothing
173
179
                }
174
180
        } else { // If we had more Orderings we could add them here
175
181
                switch l.Sort {
176
 
                case "name":
 
182
                case sortByName:
177
183
                        sort.Sort(byName(l))
178
 
                case "size":
 
184
                case sortBySize:
179
185
                        sort.Sort(bySize(l))
180
 
                case "time":
 
186
                case sortByTime:
181
187
                        sort.Sort(byTime(l))
182
188
                default:
183
189
                        // If not one of the above, do nothing
186
192
        }
187
193
}
188
194
 
189
 
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listing, bool) {
 
195
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config *Config) (Listing, bool) {
190
196
        var (
191
197
                fileinfos           []FileInfo
192
198
                dirCount, fileCount int
212
218
 
213
219
                url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
214
220
 
 
221
                if config.Fs.IsHidden(f) {
 
222
                        continue
 
223
                }
 
224
 
215
225
                fileinfos = append(fileinfos, FileInfo{
216
226
                        IsDir:   f.IsDir(),
217
227
                        Name:    f.Name(),
247
257
inScope:
248
258
 
249
259
        // Browse works on existing directories; delegate everything else
250
 
        requestedFilepath, err := bc.Root.Open(r.URL.Path)
 
260
        requestedFilepath, err := bc.Fs.Root.Open(r.URL.Path)
251
261
        if err != nil {
252
262
                switch {
253
263
                case os.IsPermission(err):
295
305
        return b.ServeListing(w, r, requestedFilepath, bc)
296
306
}
297
307
 
298
 
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string) (*Listing, bool, error) {
 
308
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string, config *Config) (*Listing, bool, error) {
299
309
        files, err := requestedFilepath.Readdir(-1)
300
310
        if err != nil {
301
311
                return nil, false, err
312
322
        }
313
323
 
314
324
        // Assemble listing of directory contents
315
 
        listing, hasIndex := directoryListing(files, canGoUp, urlPath)
 
325
        listing, hasIndex := directoryListing(files, canGoUp, urlPath, config)
316
326
 
317
327
        return &listing, hasIndex, nil
318
328
}
327
337
        // If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
328
338
        switch sort {
329
339
        case "":
330
 
                sort = "name"
 
340
                sort = sortByName
331
341
                if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
332
342
                        sort = sortCookie.Value
333
343
                }
334
 
        case "name", "size", "type":
 
344
        case sortByName, sortBySize, sortByTime:
335
345
                http.SetCookie(w, &http.Cookie{Name: "sort", Value: sort, Path: scope, Secure: r.TLS != nil})
336
346
        }
337
347
 
357
367
 
358
368
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
359
369
func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFilepath http.File, bc *Config) (int, error) {
360
 
        listing, containsIndex, err := b.loadDirectoryContents(requestedFilepath, r.URL.Path)
 
370
        listing, containsIndex, err := b.loadDirectoryContents(requestedFilepath, r.URL.Path, bc)
361
371
        if err != nil {
362
372
                switch {
363
373
                case os.IsPermission(err):
372
382
                return b.Next.ServeHTTP(w, r)
373
383
        }
374
384
        listing.Context = httpserver.Context{
375
 
                Root: bc.Root,
 
385
                Root: bc.Fs.Root,
376
386
                Req:  r,
377
387
                URL:  r.URL,
378
388
        }