/
content-types.R
100 lines (88 loc) · 3.02 KB
/
content-types.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# FROM Shiny
# @author Shiny package authors
knownContentTypes <- c(
html = "text/html; charset=UTF-8",
htm = "text/html; charset=UTF-8",
js = "text/javascript",
css = "text/css",
png = "image/png",
jpg = "image/jpeg",
jpeg = "image/jpeg",
gif = "image/gif",
svg = "image/svg+xml",
txt = "text/plain",
pdf = "application/pdf",
ps = "application/postscript",
xml = "application/xml",
m3u = "audio/x-mpegurl",
m4a = "audio/mp4a-latm",
m4b = "audio/mp4a-latm",
m4p = "audio/mp4a-latm",
mp3 = "audio/mpeg",
wav = "audio/x-wav",
m4u = "video/vnd.mpegurl",
m4v = "video/x-m4v",
mp4 = "video/mp4",
mpeg = "video/mpeg",
mpg = "video/mpeg",
avi = "video/x-msvideo",
mov = "video/quicktime",
ogg = "application/ogg",
swf = "application/x-shockwave-flash",
doc = "application/msword",
xls = "application/vnd.ms-excel",
ppt = "application/vnd.ms-powerpoint",
xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
xltx = "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
potx = "application/vnd.openxmlformats-officedocument.presentationml.template",
ppsx = "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
pptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation",
sldx = "application/vnd.openxmlformats-officedocument.presentationml.slide",
docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
dotx = "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
xlam = "application/vnd.ms-excel.addin.macroEnabled.12",
xlsb = "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
feather = "application/vnd.apache.arrow.file",
parquet = "application/vnd.apache.parquet",
rds = "application/rds",
tsv = "application/tab-separated-values",
csv = "application/csv",
json = "application/json",
yml = "application/yaml",
yaml = "application/yaml"
)
getContentType <- function(ext, defaultType = 'application/octet-stream') {
ext <- tolower(ext)
ret <-
knownContentTypes[ext] %|%
mime::mimemap[ext] %|%
defaultType
ret[[1]]
}
cleanup_content_type <- function(type) {
if (length(type) == 0) return(type)
type <- tolower(type)
# remove trailing content type information
# "text/yaml; charset=UTF-8"
# to
# "text/yaml"
if (stri_detect_fixed(type, ";")) {
type <- stri_split_fixed(type, ";")[[1]][1]
}
type
}
get_fileext <- function(type) {
type <- cleanup_content_type(type)
all_content_types <- c(knownContentTypes, mime::mimemap)
type_to_ext <- setNames(names(all_content_types), all_content_types)
ret <- type_to_ext[type] %|% NULL
ret[[1]]
}
#' Request character set
#' @param content_type Request Content-Type header
#' @return Default to `UTF-8`. Otherwise return `charset` defined in request header.
#' @export
get_character_set <- function(content_type = NULL) {
if (is.null(content_type)) return("UTF-8")
stri_match_first_regex(paste(content_type,"; charset=UTF-8"), "charset=([^;\\s]*)")[,2]
}