-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Shivam010/pfs
Add proxied file system blob implementation with its route handler
- Loading branch information
Showing
15 changed files
with
383 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package handler | ||
|
||
import ( | ||
"errors" | ||
"github.com/Shivam010/upload" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func BucketRouteAndHandler(buck *upload.Bucket) (route string, handler func(http.ResponseWriter, *http.Request), err error) { | ||
if buck == nil || buck.Provider() != upload.ProxiedFileSystem { | ||
return "", nil, errors.New("handler: can only work on Proxied File System Bucket provider") | ||
} | ||
// Bucket region is alias for the route | ||
// Bucket account is alias for the storage directory | ||
route = "/" + buck.GetMetadata("route") | ||
handlerFunc := http.StripPrefix( | ||
route, | ||
http.FileServer(wrappedFileSystem{ | ||
fs: http.Dir(buck.GetMetadata("storage")), | ||
}), | ||
).ServeHTTP | ||
|
||
return route + "/", | ||
func(w http.ResponseWriter, r *http.Request) { | ||
// security and caching headers | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.Header().Set("Referrer-Policy", "strict-origin") | ||
w.Header().Set("X-XSS-Protection", "1; mode=block") | ||
w.Header().Set("X-Frame-Options", "SAMEORIGIN") | ||
w.Header().Set("Strict-Transport-Security", "max-age=31536000") | ||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") | ||
handlerFunc(w, r) | ||
}, | ||
nil | ||
} | ||
|
||
type wrappedFileSystem struct { | ||
fs http.FileSystem | ||
} | ||
|
||
func (w wrappedFileSystem) Open(path string) (http.File, error) { | ||
file, err := w.fs.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
st, err := file.Stat() | ||
if st.IsDir() { | ||
return nil, os.ErrNotExist | ||
} | ||
return file, nil | ||
} |
Oops, something went wrong.