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