Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to share folder for view rendering #3057

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions frontend/src/api/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ export async function create(url, password = "", expires = "", unit = "hours") {
export function getShareURL(share) {
return createURL("share/" + share.hash, {}, false);
}
export function getViewURL(share) {
return createURL("api/public/view/" + share.hash + "/index.html", {}, false);
}
13 changes: 13 additions & 0 deletions frontend/src/components/prompts/Share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
<i class="material-icons">content_paste</i>
</button>
</td>
<td class="small" v-if="!hasDownloadLink()">
<button
class="action copy-clipboard"
:data-clipboard-text="buildViewLink(link)"
:aria-label="$t('buttons.copyToClipboard')"
:title="$t('buttons.copyToClipboard')"
>
<i class="material-icons">content_paste_search</i>
</button>
</td>
<td class="small" v-if="hasDownloadLink()">
<button
class="action copy-clipboard"
Expand Down Expand Up @@ -225,6 +235,9 @@ export default {
buildLink(share) {
return api.getShareURL(share);
},
buildViewLink(share) {
return api.getViewURL(share);
},
hasDownloadLink() {
return (
this.selected.length === 1 && !this.req.items[this.selected[0]].isDir
Expand Down
1 change: 1 addition & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewHandler(

public := api.PathPrefix("/public").Subrouter()
public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET")
public.PathPrefix("/view").Handler(monkey(publicViewHandler, "/api/public/view/")).Methods("GET")
public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET")

return stripPrefix(server.BaseURL, r), nil
Expand Down
9 changes: 9 additions & 0 deletions http/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ var publicDlHandler = withHashFile(func(w http.ResponseWriter, r *http.Request,
return rawDirHandler(w, r, d, file)
})

var publicViewHandler = withHashFile(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
file := d.raw.(*files.FileInfo)
if !file.IsDir {
return rawFileHandler(w, r, file, false)
}

return rawDirHandler(w, r, d, file, false)
})

func authenticateShareRequest(r *http.Request, l *share.Link) (int, error) {
if l.PasswordHash == "" {
return 0, nil
Expand Down
15 changes: 11 additions & 4 deletions http/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
return nil
}

func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.FileInfo) (int, error) {
func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.FileInfo, setContentDispositionOptional ...bool) (int, error) {
filenames, err := parseQueryFiles(r, file, d.user)
if err != nil {
return http.StatusInternalServerError, err
Expand Down Expand Up @@ -187,7 +187,10 @@ func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.
name = "_" + name
}
name += extension
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))

if len(setContentDispositionOptional) == 0 || (len(setContentDispositionOptional) > 0 && setContentDispositionOptional[0]) {
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
}

for _, fname := range filenames {
err = addFile(ar, d, fname, commonDir)
Expand All @@ -199,14 +202,18 @@ func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.
return 0, nil
}

func rawFileHandler(w http.ResponseWriter, r *http.Request, file *files.FileInfo) (int, error) {
func rawFileHandler(w http.ResponseWriter, r *http.Request, file *files.FileInfo, setContentDispositionOptional ...bool) (int, error) {
fd, err := file.Fs.Open(file.Path)
if err != nil {
return http.StatusInternalServerError, err
}
defer fd.Close()

setContentDisposition(w, r, file)
// Check if setContentDispositionOptional is provided and false
if len(setContentDispositionOptional) == 0 || (len(setContentDispositionOptional) > 0 && setContentDispositionOptional[0]) {
setContentDisposition(w, r, file)
}

w.Header().Add("Content-Security-Policy", `script-src 'none';`)
w.Header().Set("Cache-Control", "private")
http.ServeContent(w, r, file.Name, file.ModTime, fd)
Expand Down