Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import net.http
|
||||
import json
|
||||
|
||||
struct MimeType {
|
||||
source string
|
||||
extensions []string
|
||||
compressible bool
|
||||
charset string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mime_folder := dir(executable())
|
||||
chdir(mime_folder)!
|
||||
//
|
||||
mt_json := http.get('https://raw.githubusercontent.com/jshttp/mime-db/master/db.json')!
|
||||
mt_map := json.decode(map[string]MimeType, mt_json.body)!
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
ext_to_mt_str['v'] = 'text/x-vlang'
|
||||
ext_to_mt_str['vsh'] = 'text/x-vlang'
|
||||
|
||||
write_file('db.v', '
|
||||
module mime
|
||||
|
||||
// FILE AUTOGENERATED BY `build.vsh` - DO NOT MANUALLY EDIT
|
||||
|
||||
const db = ${mt_map}
|
||||
|
||||
const ext_to_mt_str = ${ext_to_mt_str}
|
||||
')!
|
||||
execute('${@VEXE} fmt -w db.v')
|
||||
println('db.v was regenerated. New file size: ${file_size('db.v')} bytes .')
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user