vweb: detect mime type automatically in serve_static()

pull/10167/head
Alexander Medvednikov 2021-05-22 07:02:21 +03:00
parent 788520eb52
commit dcfea5c69b
3 changed files with 8 additions and 5 deletions

View File

@ -15,7 +15,7 @@ fn main() {
} }
pub fn (mut app App) init_server() { pub fn (mut app App) init_server() {
app.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon') app.serve_static('/favicon.ico', 'favicon.ico')
app.mount_static_folder_at(os.resource_abs_path('.'), '/') app.mount_static_folder_at(os.resource_abs_path('.'), '/')
} }

View File

@ -18,7 +18,7 @@ fn main() {
pub fn (mut app App) init_server() { pub fn (mut app App) init_server() {
// Arbitary mime type. // Arbitary mime type.
app.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon') app.serve_static('/favicon.ico', 'favicon.ico')
// Automatically make available known static mime types found in given directory. // Automatically make available known static mime types found in given directory.
// app.handle_static('assets') // app.handle_static('assets')
// This would make available all known static mime types from current // This would make available all known static mime types from current

View File

@ -35,6 +35,7 @@ pub const (
'.txt': 'text/plain; charset=utf-8' '.txt': 'text/plain; charset=utf-8'
'.wasm': 'application/wasm' '.wasm': 'application/wasm'
'.xml': 'text/xml; charset=utf-8' '.xml': 'text/xml; charset=utf-8'
'.ico': 'img/x-icon'
} }
max_http_post_size = 1024 * 1024 max_http_post_size = 1024 * 1024
default_port = 8080 default_port = 8080
@ -558,7 +559,7 @@ fn (mut ctx Context) scan_static_directory(directory_path string, mount_path str
// Rudimentary guard against adding files not in mime_types. // Rudimentary guard against adding files not in mime_types.
// Use serve_static directly to add non-standard mime types. // Use serve_static directly to add non-standard mime types.
if ext in vweb.mime_types { if ext in vweb.mime_types {
ctx.serve_static(mount_path + '/' + file, full_path, vweb.mime_types[ext]) ctx.serve_static(mount_path + '/' + file, full_path)
} }
} }
} }
@ -596,9 +597,11 @@ pub fn (mut ctx Context) mount_static_folder_at(directory_path string, mount_pat
// Serves a file static // Serves a file static
// `url` is the access path on the site, `file_path` is the real path to the file, `mime_type` is the file type // `url` is the access path on the site, `file_path` is the real path to the file, `mime_type` is the file type
pub fn (mut ctx Context) serve_static(url string, file_path string, mime_type string) { pub fn (mut ctx Context) serve_static(url string, file_path string) {
ctx.static_files[url] = file_path ctx.static_files[url] = file_path
ctx.static_mime_types[url] = mime_type // ctx.static_mime_types[url] = mime_type
ext := os.file_ext(file_path)
ctx.static_mime_types[url] = vweb.mime_types[ext]
} }
// Returns the ip address from the current user // Returns the ip address from the current user