File_server default template force download

1. Caddy version (caddy version):

2.3

2. How I run Caddy:

a. System environment:

Docker on Unraid

b. Command:

Docker on Unraid

c. Service/unit/compose file:

Docker

d. My complete Caddyfile or JSON config:

my.site.com
basicauth /* {
	
}
bind {$ADDRESS}
tls /certs/certificate.crt /certs/private.key
root * /var/www
file_server browse
log {
	output file         /certs/access.log
	format single_field common_log
}

3. The problem I’m having:

file_server works correctly, however some files open in a browser instead of downloading. I’d prefer users to click a link and download instead of browser opening, such as an mp4 and then it currently streams instead of downloads. Users can rightclick and save video as. Previously I had caddy1 and used a template file, however I find that caddyv2 file_server being much faster without a template file I was using previously. Previously I got around this via this download code in the a href link.

<a download href="{{.URL}}">{{.Name}}</a>

Is there a way to edit the default template to have this effect?

4. Error messages and/or full log output:

None

5. What I already tried:

I tried my previous template file, it works however about 5x slower than the built in file_server template. I looked for the default template as well as at others. I prefer the built in, just with this download in the a href if possible. Is there a way to copy the default template and edit it?

Let me know,

Thanks!

6. Links to relevant resources:

None

Yep, it’s here:

https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/fileserver/browsetpl.go

And you can change templates with:

file_server {
	browse <path to template>
}
1 Like

Thank you very much!

I copied the html part, placed in a template.html file, did the template change you mentioned to the path of the template.

In the end, the only part I changed was this:

 <td>
							<a href="{{html .URL}}">
								{{- if .IsDir}}
								<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 317 259"><use xlink:href="#folder{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
								{{- else}}
								<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 265 323"><use xlink:href="#file{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
								{{- end}}
								<span class="name">{{html .Name}}</span>
							</a>
						</td>

to be this

<td>
								{{- if .IsDir}}
								<a href="{{html .URL}}">
								<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 317 259"><use xlink:href="#folder{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
								
								{{- else}}
								<a download href="{{html .URL}}">
								<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 265 323"><use xlink:href="#file{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
								{{- end}}
								<span class="name">{{html .Name}}</span>
								</a>
						</td>

Moved the <a> tag inside both if / else, then end at the bottom. The else is the files and the first if is the directory. Only wanted it to apply to files, not folders so you can still traverse.

Seems to be working like a charm!

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.