Skip to content

Commit

Permalink
Fixes (dutchcoders#149)
Browse files Browse the repository at this point in the history
* Head handlers & Update README.md
  • Loading branch information
limitedeternity authored and aspacca committed Aug 13, 2018
1 parent a640bcf commit 8df04e3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 DutchCoders [https://github.com/dutchcoders/]
Copyright (c) 2014-2018 DutchCoders [https://github.com/dutchcoders/]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ Transfer.sh currently supports the s3 (Amazon S3), gdrive (Google Drive) provide

```
Upload:
$ curl --upload-file ./hello.txt https://transfer.sh/hello.txt
Encrypt & upload:
$ cat /tmp/hello.txt|gpg -ac -o-|curl -X PUT --upload-file "-" https://transfer.sh/test.txt
Download & decrypt:
$ curl https://transfer.sh/1lDau/test.txt|gpg -o- > /tmp/hello.txt
Upload to virustotal:
$ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal
```
Expand All @@ -33,6 +37,16 @@ Now run it like this
$ transfer test.txt
```

## Link aliases

Create direct download link:

https://transfer.sh/1lDau/test.txt --> https://transfer.sh/get/1lDau/test.txt

Inline file:

https://transfer.sh/1lDau/test.txt --> https://transfer.sh/inline/1lDau/test.txt

### On Windows

Put a file called transfer.cmd somewhere in your PATH with this inside it:
Expand Down Expand Up @@ -70,7 +84,7 @@ basedir | path storage for local/gdrive provider| |
gdrive-client-json-filepath | path to client json config for gdrive provider| |
gdrive-local-config-path | path to local transfer.sh config cache for gdrive provider| |
lets-encrypt-hosts | hosts to use for lets encrypt certificates (comma seperated) | |
log | path to log file| |
log | path to log file| |

If you want to use TLS using lets encrypt certificates, set lets-encrypt-hosts to your domain, set tls-listener to :443 and enable force-https.

Expand All @@ -81,7 +95,7 @@ If you want to use TLS using your own certificates, set tls-listener to :443, fo
Make sure your GOPATH is set correctly.

```
go run main.go -provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/
go run main.go -provider=local --listener :8080 --temp-path=/tmp/ --basedir=/tmp/
```

## Build
Expand All @@ -102,7 +116,7 @@ docker run --publish 8080:8080 dutchcoders/transfer.sh:latest --provider local -

Contributions are welcome.

## Creators
## Creators

**Remco Verhoef**
- <https://twitter.com/remco_verhoef>
Expand All @@ -112,5 +126,5 @@ Contributions are welcome.

## Copyright and license

Code and documentation copyright 2011-2014 Remco Verhoef.
Code released under [the MIT license](LICENSE).
Code and documentation copyright 2011-2014 Remco Verhoef.
Code released under [the MIT license](LICENSE).
40 changes: 38 additions & 2 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,37 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) headHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

token := vars["token"]
filename := vars["filename"]

if err := s.CheckMetadata(token, filename); err != nil {
log.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}

contentType, contentLength, err := s.storage.Head(token, filename)
if s.storage.IsNotExist(err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else if err != nil {
log.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
}

w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10))
w.Header().Set("Connection", "close")
}

func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

action := vars["action"]
token := vars["token"]
filename := vars["filename"]

Expand All @@ -815,10 +843,18 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {

defer reader.Close()

var disposition string

if action == "inline" {
disposition = "inline"
} else {
disposition = "attachment"
}

w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatUint(contentLength, 10))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
w.Header().Set("Connection", "close")
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, filename))
w.Header().Set("Connection", "keep-alive")

if _, err = io.Copy(w, reader); err != nil {
log.Printf("%s", err.Error())
Expand Down
6 changes: 4 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ func (s *Server) Run() {
r.HandleFunc("/({files:.*}).tar", s.tarHandler).Methods("GET")
r.HandleFunc("/({files:.*}).tar.gz", s.tarGzHandler).Methods("GET")

r.HandleFunc("/{token}/{filename}", s.headHandler).Methods("HEAD")
r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", s.headHandler).Methods("HEAD")

r.HandleFunc("/{token}/{filename}", s.previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) (match bool) {
match = false

Expand Down Expand Up @@ -352,8 +355,7 @@ func (s *Server) Run() {
}

r.HandleFunc("/{token}/{filename}", getHandlerFn).Methods("GET")
r.HandleFunc("/get/{token}/{filename}", getHandlerFn).Methods("GET")
r.HandleFunc("/download/{token}/{filename}", getHandlerFn).Methods("GET")
r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", getHandlerFn).Methods("GET")

r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT")
r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT")
Expand Down

0 comments on commit 8df04e3

Please sign in to comment.