Fix 500 errors on file routes when accessing a file that doesn't exist

This commit is contained in:
Matthew Penner
2020-07-31 16:01:32 -06:00
parent 373dbd355e
commit e85b1cecb7
3 changed files with 36 additions and 6 deletions

View File

@@ -40,8 +40,8 @@ func IsPathResolutionError(err error) bool {
}
type Filesystem struct {
Server *Server
cacheDiskMu sync.Mutex
Server *Server
cacheDiskMu sync.Mutex
}
// Returns the root path that contains all of a server's data.
@@ -424,7 +424,7 @@ func (fs *Filesystem) unsafeStat(p string) (*Stat, error) {
return st, nil
}
// Creates a new directory (name) at a specificied path (p) for the server.
// Creates a new directory (name) at a specified path (p) for the server.
func (fs *Filesystem) CreateDirectory(name string, p string) error {
cleaned, err := fs.SafePath(path.Join(p, name))
if err != nil {
@@ -540,7 +540,7 @@ func (fs *Filesystem) Copy(p string) error {
}
if s, err := os.Stat(cleaned); err != nil {
return err
return errors.WithStack(err)
} else if s.IsDir() || !s.Mode().IsRegular() {
// If this is a directory or not a regular file, just throw a not-exist error
// since anything calling this function should understand what that means.

View File

@@ -73,6 +73,11 @@ func (fs *Filesystem) DecompressFile(dir string, file string) error {
return errors.WithStack(err)
}
// Make sure the file exists basically.
if _, err := os.Stat(source); err != nil {
return errors.WithStack(err)
}
// Walk over all of the files spinning up an additional go-routine for each file we've encountered
// and then extract that file from the archive and write it to the disk. If any part of this process
// encounters an error the entire process will be stopped.