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