From dcfea5c69bdb7e92555cd23f7ab9451fa1513ece Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 22 May 2021 07:02:21 +0300 Subject: [PATCH] vweb: detect mime type automatically in serve_static() --- examples/vweb/server_sent_events/server.v | 2 +- examples/vweb/vweb_assets/vweb_assets.v | 2 +- vlib/vweb/vweb.v | 9 ++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/vweb/server_sent_events/server.v b/examples/vweb/server_sent_events/server.v index 4d0c4b43a4..01e63cbb76 100644 --- a/examples/vweb/server_sent_events/server.v +++ b/examples/vweb/server_sent_events/server.v @@ -15,7 +15,7 @@ fn main() { } 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('.'), '/') } diff --git a/examples/vweb/vweb_assets/vweb_assets.v b/examples/vweb/vweb_assets/vweb_assets.v index 714b1486a8..05d5da0ae4 100644 --- a/examples/vweb/vweb_assets/vweb_assets.v +++ b/examples/vweb/vweb_assets/vweb_assets.v @@ -18,7 +18,7 @@ fn main() { pub fn (mut app App) init_server() { // 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. // app.handle_static('assets') // This would make available all known static mime types from current diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 9079ffaaf3..7d3adccbb0 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -35,6 +35,7 @@ pub const ( '.txt': 'text/plain; charset=utf-8' '.wasm': 'application/wasm' '.xml': 'text/xml; charset=utf-8' + '.ico': 'img/x-icon' } max_http_post_size = 1024 * 1024 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. // Use serve_static directly to add non-standard 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 // `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_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