FahlGrahn Audio v1.0.0
Loading...
Searching...
No Matches
WebViewUtilities.h
1#include <vector>
2
3#if defined NDEBUG
4static std::vector<std::byte> streamToVector(juce::InputStream &stream)
5{
6 using namespace juce;
7 const auto sizeInBytes = static_cast<size_t>(stream.getTotalLength());
8 std::vector<std::byte> result(sizeInBytes);
9 stream.setPosition(0);
10 [[maybe_unused]] const auto bytesRead = stream.read(result.data(), result.size());
11 jassert(bytesRead == static_cast<ssize_t>(sizeInBytes));
12 return result;
13}
14
15#endif
16
17static std::vector<std::byte> getWebViewFileAsBytes(const juce::String &filepath)
18{
19 std::ignore = filepath;
20#if defined NDEBUG
21 juce::MemoryInputStream zipStream{webview_files::webview_files_zip, webview_files::webview_files_zipSize, false};
22 juce::ZipFile zipFile{zipStream};
23
24 if (auto *zipEntry = zipFile.getEntry(ZIPPED_FILES_PREFIX + filepath))
25 {
26 const std::unique_ptr<juce::InputStream> entryStream{zipFile.createStreamForEntry(*zipEntry)};
27
28 if (entryStream == nullptr)
29 {
30 jassertfalse;
31 return {};
32 }
33
34 return streamToVector(*entryStream);
35 }
36#endif
37
38 return {};
39}
40
41static const char *getMimeForExtension(const juce::String &extension)
42{
43 static const std::unordered_map<juce::String, const char *> mimeMap = {{{"htm"}, "text/html"},
44 {{"html"}, "text/html"},
45 {{"txt"}, "text/plain"},
46 {{"jpg"}, "image/jpeg"},
47 {{"jpeg"}, "image/jpeg"},
48 {{"svg"}, "image/svg+xml"},
49 {{"ico"}, "image/vnd.microsoft.icon"},
50 {{"json"}, "application/json"},
51 {{"png"}, "image/png"},
52 {{"css"}, "text/css"},
53 {{"map"}, "application/json"},
54 {{"js"}, "text/javascript"},
55 {{"woff2"}, "font/woff2"}};
56
57 if (const auto it = mimeMap.find(extension.toLowerCase()); it != mimeMap.end())
58 return it->second;
59
60 jassertfalse;
61 return "";
62}