make everything more readble
parent
3baf7537d9
commit
63a1a2cd54
|
@ -2,7 +2,6 @@ package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -40,23 +39,18 @@ type Listing struct {
|
||||||
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
|
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// Gets the directory information using the Virtual File System of
|
||||||
|
// the user configuration
|
||||||
file, err := u.FileSystem.OpenFile(i.VirtualPath, os.O_RDONLY, 0)
|
file, err := u.FileSystem.OpenFile(i.VirtualPath, os.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.ErrorToHTTPCode(err, true), err
|
return utils.ErrorToHTTPCode(err, true), err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// Loads the content of the directory
|
||||||
listing, err := i.loadDirectoryContents(file, r.URL.Path, u)
|
listing, err := i.loadDirectoryContents(file, r.URL.Path, u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return utils.ErrorToHTTPCode(err, true), err
|
||||||
switch {
|
|
||||||
case os.IsPermission(err):
|
|
||||||
return http.StatusForbidden, err
|
|
||||||
case os.IsExist(err):
|
|
||||||
return http.StatusGone, err
|
|
||||||
default:
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listing.Context = httpserver.Context{
|
listing.Context = httpserver.Context{
|
||||||
|
@ -111,17 +105,12 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co
|
||||||
return page.PrintAsHTML(w, "listing")
|
return page.PrintAsHTML(w, "listing")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Info) loadDirectoryContents(file http.File, path string, u *config.User) (*Listing, error) {
|
func (i Info) loadDirectoryContents(file http.File, basePath string, u *config.User) (*Listing, error) {
|
||||||
files, err := file.Readdir(-1)
|
files, err := file.Readdir(-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
listing := directoryListing(files, i.VirtualPath, path, u)
|
|
||||||
return &listing, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func directoryListing(files []os.FileInfo, urlPath string, basePath string, u *config.User) Listing {
|
|
||||||
var (
|
var (
|
||||||
fileinfos []Info
|
fileinfos []Info
|
||||||
dirCount, fileCount int
|
dirCount, fileCount int
|
||||||
|
@ -146,11 +135,11 @@ func directoryListing(files []os.FileInfo, urlPath string, basePath string, u *c
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return Listing{
|
return &Listing{
|
||||||
Name: path.Base(urlPath),
|
Name: path.Base(i.VirtualPath),
|
||||||
Path: urlPath,
|
Path: i.VirtualPath,
|
||||||
Items: fileinfos,
|
Items: fileinfos,
|
||||||
NumDirs: dirCount,
|
NumDirs: dirCount,
|
||||||
NumFiles: fileCount,
|
NumFiles: fileCount,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue