-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlobPacking.jl
More file actions
90 lines (76 loc) · 2.88 KB
/
BlobPacking.jl
File metadata and controls
90 lines (76 loc) · 2.88 KB
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
# using FileIO
# using ImageIO
# using LasIO
# using BSON
# using OrderedCollections
# 2 types for now with MIME type
# 1. JSON - application/octet-stream/json
# 2. FileIO - application/octet-stream
# - application/bson
# - image/jpeg
# - image/png
# - application/vnd.apache.arrow.file
const _MIMETypes = OrderedDict{MIME, DataType}()
push!(_MIMETypes, MIME("application/octet-stream/json") => format"JSON")
push!(_MIMETypes, MIME("application/bson") => format"BSON")
push!(_MIMETypes, MIME("image/png") => format"PNG")
push!(_MIMETypes, MIME("image/jpeg") => format"JPG")
push!(_MIMETypes, MIME("application/vnd.las") => format"LAS")
push!(_MIMETypes, MIME("application/vnd.apache.parque") => format"Parquet") # Provided by FileIO with ParquetFiles
"""
packBlob
Convert a file (JSON, JPG, PNG, BSON, LAS) to Vector{UInt8} for use as a Blob.
Returns the blob and MIME type.
"""
function packBlob end
"""
unpackBlob
Convert a Blob back to the origanal typ using the MIME type or DataFormat type.
"""
function unpackBlob end
unpackBlob(mime::String, blob) = unpackBlob(MIME(mime), blob)
function unpackBlob(T::MIME, blob)
dataformat = get(_MIMETypes, T, nothing)
isnothing(dataformat) && error("Format not found for MIME type $(T)")
return unpackBlob(dataformat, blob)
end
# 1. JSON strings are saved as is
function packBlob(::Type{format"JSON"}, json_str::String)
mimetype = findfirst(==(format"JSON"), _MIMETypes)
# blob = codeunits(json_str)
blob = Vector{UInt8}(json_str)
return blob, mimetype
end
function unpackBlob(::Type{format"JSON"}, blob::Vector{UInt8})
return String(copy(blob))
end
unpackBlob(entry::Blobentry, blob::Vector{UInt8}) = unpackBlob(entry.mimetype, blob)
unpackBlob(eb::Pair{<:Blobentry, Vector{UInt8}}) = unpackBlob(eb[1], eb[2])
# 2/ FileIO
function packBlob(::Type{T}, data::Any; kwargs...) where {T <: DataFormat}
io = IOBuffer()
save(Stream{T}(io), data; kwargs...)
blob = take!(io)
mimetype = findfirst(==(T), _MIMETypes)
if isnothing(mimetype)
@warn "No MIME type found for format $T"
mimetype = MIME"application/octet-stream"
end
return blob, mimetype
end
function unpackBlob(::Type{T}, blob::Vector{UInt8}) where {T <: DataFormat}
io = IOBuffer(blob)
return load(Stream{T}(io))
end
# if false
# json_str = "{\"name\":\"John\"}"
# blob, mimetype = packBlob(format"JSON", json_str)
# @assert json_str == unpackBlob(format"JSON", blob)
# @assert json_str == unpackBlob(MIME("application/octet-stream/json"), blob)
# @assert json_str == unpackBlob("application/octet-stream/json", blob)
# blob,mime = packBlob(format"PNG", img)
# up_img = unpackBlob(format"PNG", blob)
# #TODO BSON does not work yet, can extend [un]packBlob(::Type{format"BSON"}, ...)
# packBlob(format"BSON", Dict("name"=>"John"))
# unpackBlob(format"BSON", Dict("name"=>"John"))
# end