mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2025-01-11 11:20:18 +01:00
improved error handlig for not existing files
This commit is contained in:
parent
2ea3f89cee
commit
1b562e1db0
2 changed files with 19 additions and 5 deletions
|
@ -393,7 +393,7 @@ func zipHandler(w http.ResponseWriter, r *http.Request) {
|
|||
reader, _, _, err := storage.Get(token, filename)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
if storage.IsNotExist(err) {
|
||||
http.Error(w, "File not found", 404)
|
||||
return
|
||||
} else {
|
||||
|
@ -463,7 +463,7 @@ func tarGzHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
reader, _, contentLength, err := storage.Get(token, filename)
|
||||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
if storage.IsNotExist(err) {
|
||||
http.Error(w, "File not found", 404)
|
||||
return
|
||||
} else {
|
||||
|
@ -515,7 +515,7 @@ func tarHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
reader, _, contentLength, err := storage.Get(token, filename)
|
||||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
if storage.IsNotExist(err) {
|
||||
http.Error(w, "File not found", 404)
|
||||
return
|
||||
} else {
|
||||
|
@ -555,7 +555,7 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
reader, contentType, contentLength, err := storage.Get(token, filename)
|
||||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
if storage.IsNotExist(err) {
|
||||
http.Error(w, "File not found", 404)
|
||||
return
|
||||
} else {
|
||||
|
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/goamz/goamz/s3"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
|
@ -11,12 +10,15 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/goamz/goamz/s3"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
Get(token string, filename string) (reader io.ReadCloser, contentType string, contentLength uint64, err error)
|
||||
Head(token string, filename string) (contentType string, contentLength uint64, err error)
|
||||
Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error
|
||||
IsNotExist(err error) bool
|
||||
}
|
||||
|
||||
type LocalStorage struct {
|
||||
|
@ -63,6 +65,10 @@ func (s *LocalStorage) Get(token string, filename string) (reader io.ReadCloser,
|
|||
return
|
||||
}
|
||||
|
||||
func (s *LocalStorage) IsNotExist(err error) bool {
|
||||
return os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (s *LocalStorage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error {
|
||||
var f io.WriteCloser
|
||||
var err error
|
||||
|
@ -120,6 +126,14 @@ func (s *S3Storage) Head(token string, filename string) (contentType string, con
|
|||
return
|
||||
}
|
||||
|
||||
func (s *S3Storage) IsNotExist(err error) bool {
|
||||
log.Printf("IsNotExist: %s, %#v", err.Error(), err)
|
||||
|
||||
b := (err.Error() == "The specified key does not exist.")
|
||||
b = b || (err.Error() == "Access Denied")
|
||||
return b
|
||||
}
|
||||
|
||||
func (s *S3Storage) Get(token string, filename string) (reader io.ReadCloser, contentType string, contentLength uint64, err error) {
|
||||
key := fmt.Sprintf("%s/%s", token, filename)
|
||||
|
||||
|
|
Loading…
Reference in a new issue