vlib: add `net.http.mime` (#14516)

master
Hunam 2022-06-02 17:07:25 +02:00 committed by GitHub
parent aae5b9fb95
commit 41414b5d5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15115 additions and 1 deletions

View File

@ -0,0 +1,33 @@
import net.http
import json
struct MimeType {
source string
extensions []string
compressible bool
charset string
}
fn main() {
mt_json := http.get('https://raw.githubusercontent.com/jshttp/mime-db/master/db.json')?
mt_map := json.decode(map[string]MimeType, mt_json.text)?
mut ext_to_mt_str := map[string]string{}
for mt_str, mt in mt_map {
for ext in mt.extensions {
ext_to_mt_str[ext] = mt_str
}
}
write_file('db.v', '
module mime
// FILE AUTOGENERATED BY `build.vsh` - DO NOT MANUALLY EDIT
const (
db = $mt_map
ext_to_mt_str = $ext_to_mt_str
)
')?
execute('${@VEXE} fmt -w db.v')
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,39 @@
module mime
pub struct MimeType {
source string
extensions []string
compressible bool
charset string
}
// returns a `MimeType` for the given MIME type
pub fn get_complete_mime_type(mt string) MimeType {
return db[mt]
}
// returns the MIME type for the given file extension
pub fn get_mime_type(ext string) string {
return ext_to_mt_str[ext]
}
// returns a `content-type` header ready to use for the given MIME type
pub fn get_content_type(mt string) string {
mt_struct := db[mt]
charset := if mt_struct.charset.len > 0 { mt_struct.charset.to_lower() } else { 'utf-8' }
return '$mt; charset=$charset'
}
// returns the default extension for the given MIME type
pub fn get_default_ext(mt string) string {
return if db[mt].extensions.len > 0 {
db[mt].extensions[0]
} else {
''
}
}
// returns true if the given MIME type exists
pub fn exists(mt string) bool {
return mt in db
}

View File

@ -0,0 +1,29 @@
module mime
fn test_mime() {
assert get_complete_mime_type('application/json') == MimeType{
source: 'iana'
extensions: ['json', 'map']
compressible: true
charset: 'UTF-8'
}
assert get_mime_type('json') == 'application/json'
assert get_content_type('application/json') == 'application/json; charset=utf-8'
assert get_default_ext('application/json') == 'json'
assert get_complete_mime_type('text/markdown') == MimeType{
source: 'iana'
extensions: ['md', 'markdown']
compressible: true
charset: ''
}
assert get_mime_type('md') == 'text/markdown'
assert get_content_type('text/markdown') == 'text/markdown; charset=utf-8'
assert get_default_ext('text/markdown') == 'md'
assert exists('application/json') == true
assert exists('udfsbsfib') == false
assert get_default_ext('application/1d-interleaved-parityfec') == '' // valid mime type without associated extension
assert get_default_ext('invalid mime type') == '' // invalid mime type
}

View File

@ -260,7 +260,7 @@ pub fn (mut ctx Context) file(f_path string) Result {
return Result{} return Result{}
} }
content_type := vweb.mime_types[ext] content_type := vweb.mime_types[ext]
if content_type == '' { if content_type.len == O {
eprintln('no MIME type found for extension $ext') eprintln('no MIME type found for extension $ext')
ctx.server_error(500) ctx.server_error(500)
} else { } else {