@ng-native/desktop 0.0.0-stage → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
#include "ngn/expo-image.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/bytes.h"
|
|
4
|
+
#include "ngn/desktop-view-props.h"
|
|
5
|
+
#include "ngn/download.h"
|
|
6
|
+
#include "ngn/file-system.h"
|
|
7
|
+
|
|
8
|
+
#include <react/renderer/components/view/conversions.h>
|
|
9
|
+
#include <react/renderer/core/propsConversions.h>
|
|
10
|
+
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <cmath>
|
|
13
|
+
#include <filesystem>
|
|
14
|
+
#include <fstream>
|
|
15
|
+
#include <numbers>
|
|
16
|
+
|
|
17
|
+
using namespace facebook::react;
|
|
18
|
+
|
|
19
|
+
namespace ngn {
|
|
20
|
+
|
|
21
|
+
const char ExpoImageComponentName[] = "ViewManagerAdapter_ExpoImage";
|
|
22
|
+
|
|
23
|
+
ExpoImageProps::ExpoImageProps(const PropsParserContext &context, const ExpoImageProps &sourceProps, const RawProps &rawProps)
|
|
24
|
+
: ViewProps(context, sourceProps, rawProps),
|
|
25
|
+
HasDesktopProps{parseDesktopProps(context, sourceProps.desktop, rawProps)},
|
|
26
|
+
source(convertRawProp(context, rawProps, "source", sourceProps.source, nullptr)),
|
|
27
|
+
placeholder(convertRawProp(context, rawProps, "placeholder", sourceProps.placeholder, nullptr)),
|
|
28
|
+
contentFit(convertRawProp(context, rawProps, "contentFit", sourceProps.contentFit, std::string{"cover"})),
|
|
29
|
+
placeholderContentFit(
|
|
30
|
+
convertRawProp(context, rawProps, "placeholderContentFit", sourceProps.placeholderContentFit, std::string{"scale-down"})),
|
|
31
|
+
contentPosition(convertRawProp(context, rawProps, "contentPosition", sourceProps.contentPosition, nullptr)),
|
|
32
|
+
transition(convertRawProp(context, rawProps, "transition", sourceProps.transition, nullptr)),
|
|
33
|
+
blurRadius(convertRawProp(context, rawProps, "blurRadius", sourceProps.blurRadius, 0.0)),
|
|
34
|
+
tintColor(convertRawProp(context, rawProps, "tintColor", sourceProps.tintColor, {})),
|
|
35
|
+
cachePolicy(convertRawProp(context, rawProps, "cachePolicy", sourceProps.cachePolicy, std::string{"disk"})),
|
|
36
|
+
recyclingKey(convertRawProp(context, rawProps, "recyclingKey", sourceProps.recyclingKey, {})) {}
|
|
37
|
+
|
|
38
|
+
namespace {
|
|
39
|
+
|
|
40
|
+
double numberIn(const folly::dynamic &object, const char *key) {
|
|
41
|
+
const auto *value = object.isObject() ? object.get_ptr(key) : nullptr;
|
|
42
|
+
return value != nullptr && value->isNumber() ? value->asDouble() : 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
std::string textIn(const folly::dynamic &object, const char *key) {
|
|
46
|
+
const auto *value = object.isObject() ? object.get_ptr(key) : nullptr;
|
|
47
|
+
return value != nullptr && value->isString() ? value->asString() : "";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A contentPosition value as a distance in points, or as a fraction of the space left.
|
|
51
|
+
struct PositionValue {
|
|
52
|
+
std::optional<double> distance;
|
|
53
|
+
std::optional<double> factor;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
PositionValue positionValue(const folly::dynamic &position, const char *key) {
|
|
57
|
+
const auto *value = position.isObject() ? position.get_ptr(key) : nullptr;
|
|
58
|
+
if (value == nullptr) {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
if (value->isNumber()) {
|
|
62
|
+
return {.distance = value->asDouble()};
|
|
63
|
+
}
|
|
64
|
+
if (value->isString()) {
|
|
65
|
+
const auto &text = value->asString();
|
|
66
|
+
try {
|
|
67
|
+
if (!text.empty() && text.back() == '%') {
|
|
68
|
+
return {.factor = std::stod(text.substr(0, text.size() - 1)) / 100};
|
|
69
|
+
}
|
|
70
|
+
return {.distance = std::stod(text)};
|
|
71
|
+
} catch (const std::exception &) {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// expo-image's offset from the centre along one axis.
|
|
79
|
+
double offsetAlong(const folly::dynamic &position, const char *start, const char *end, double content, double container) {
|
|
80
|
+
double diff = container - content;
|
|
81
|
+
auto first = positionValue(position, start), last = positionValue(position, end);
|
|
82
|
+
if (first.distance) {
|
|
83
|
+
return -diff / 2 + *first.distance;
|
|
84
|
+
}
|
|
85
|
+
if (last.distance) {
|
|
86
|
+
return diff / 2 - *last.distance;
|
|
87
|
+
}
|
|
88
|
+
if (first.factor) {
|
|
89
|
+
return -diff / 2 + diff * *first.factor;
|
|
90
|
+
}
|
|
91
|
+
if (last.factor) {
|
|
92
|
+
return diff / 2 - diff * *last.factor;
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
int decode83(const std::string &text, size_t from, size_t to, bool &ok) {
|
|
98
|
+
static const std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~";
|
|
99
|
+
int value = 0;
|
|
100
|
+
for (size_t i = from; i < to; i++) {
|
|
101
|
+
auto digit = digits.find(text[i]);
|
|
102
|
+
if (digit == std::string::npos) {
|
|
103
|
+
ok = false;
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
value = value * 83 + static_cast<int>(digit);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
double sRGBToLinear(int value) {
|
|
112
|
+
double v = value / 255.0;
|
|
113
|
+
return v <= 0.04045 ? v / 12.92 : std::pow((v + 0.055) / 1.055, 2.4);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
uint8_t linearToSRGB(double value) {
|
|
117
|
+
double v = std::clamp(value, 0.0, 1.0);
|
|
118
|
+
double out = v <= 0.0031308 ? v * 12.92 * 255 + 0.5 : (1.055 * std::pow(v, 1 / 2.4) - 0.055) * 255 + 0.5;
|
|
119
|
+
return static_cast<uint8_t>(std::clamp(out, 0.0, 255.0));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
double signPow(double value, double exponent) {
|
|
123
|
+
return std::copysign(std::pow(std::abs(value), exponent), value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
std::string percentDecoded(const std::string &text) {
|
|
127
|
+
std::string out;
|
|
128
|
+
for (size_t i = 0; i < text.size(); i++) {
|
|
129
|
+
if (text[i] == '%' && i + 2 < text.size() && std::isxdigit(static_cast<unsigned char>(text[i + 1])) &&
|
|
130
|
+
std::isxdigit(static_cast<unsigned char>(text[i + 2]))) {
|
|
131
|
+
out += static_cast<char>(std::stoi(text.substr(i + 1, 2), nullptr, 16));
|
|
132
|
+
i += 2;
|
|
133
|
+
} else {
|
|
134
|
+
out += text[i];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
uint64_t fnv1a(const std::string &text, uint64_t seed) {
|
|
141
|
+
uint64_t hash = seed;
|
|
142
|
+
for (unsigned char c : text) {
|
|
143
|
+
hash = (hash ^ c) * 1099511628211ULL;
|
|
144
|
+
}
|
|
145
|
+
return hash;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
std::string contentsOf(const std::string &path) {
|
|
149
|
+
std::ifstream in(path, std::ios::binary);
|
|
150
|
+
if (!in) {
|
|
151
|
+
return {};
|
|
152
|
+
}
|
|
153
|
+
return {std::istreambuf_iterator<char>(in), {}};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
} // namespace
|
|
157
|
+
|
|
158
|
+
std::vector<ImageSourceRef> imageSources(const folly::dynamic &list) {
|
|
159
|
+
std::vector<ImageSourceRef> sources;
|
|
160
|
+
auto add = [&](const folly::dynamic &entry) {
|
|
161
|
+
auto uri = textIn(entry, "uri");
|
|
162
|
+
if (uri.empty()) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
ImageSourceRef source{.uri = uri, .width = numberIn(entry, "width"), .height = numberIn(entry, "height"), .cacheKey = textIn(entry, "cacheKey")};
|
|
166
|
+
if (const auto *headers = entry.get_ptr("headers"); headers != nullptr && headers->isObject()) {
|
|
167
|
+
for (const auto &[name, value] : headers->items()) {
|
|
168
|
+
if (name.isString() && value.isString()) {
|
|
169
|
+
source.headers.emplace_back(name.asString(), value.asString());
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
sources.push_back(std::move(source));
|
|
174
|
+
};
|
|
175
|
+
if (list.isArray()) {
|
|
176
|
+
for (const auto &entry : list) {
|
|
177
|
+
add(entry);
|
|
178
|
+
}
|
|
179
|
+
} else if (list.isObject()) {
|
|
180
|
+
add(list);
|
|
181
|
+
}
|
|
182
|
+
return sources;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
std::optional<ImageSourceRef> bestImageSource(const std::vector<ImageSourceRef> &sources, double width, double height, double scale) {
|
|
186
|
+
if (sources.empty() || width <= 0 || height <= 0) {
|
|
187
|
+
return std::nullopt;
|
|
188
|
+
}
|
|
189
|
+
if (sources.size() == 1) {
|
|
190
|
+
return sources.front();
|
|
191
|
+
}
|
|
192
|
+
double target = width * height * scale * scale;
|
|
193
|
+
std::optional<ImageSourceRef> best;
|
|
194
|
+
double bestFit = INFINITY;
|
|
195
|
+
for (const auto &source : sources) {
|
|
196
|
+
double fit = std::abs(1 - (source.width * source.height / target));
|
|
197
|
+
if (fit < bestFit) {
|
|
198
|
+
best = source;
|
|
199
|
+
bestFit = fit;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return best;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
ImageRect placeImage(const std::string &fit, const folly::dynamic &position, std::pair<double, double> image, std::pair<double, double> box) {
|
|
206
|
+
auto [width, height] = image;
|
|
207
|
+
auto [boxWidth, boxHeight] = box;
|
|
208
|
+
if (width <= 0 || height <= 0) {
|
|
209
|
+
return {};
|
|
210
|
+
}
|
|
211
|
+
if (fit == "fill") {
|
|
212
|
+
return {0, 0, boxWidth, boxHeight};
|
|
213
|
+
}
|
|
214
|
+
double scale = 1;
|
|
215
|
+
double fitScale = std::min(boxWidth / width, boxHeight / height);
|
|
216
|
+
if (fit == "contain") {
|
|
217
|
+
scale = fitScale;
|
|
218
|
+
} else if (fit == "scale-down") {
|
|
219
|
+
scale = std::min(1.0, fitScale);
|
|
220
|
+
} else if (fit != "none") {
|
|
221
|
+
scale = std::max(boxWidth / width, boxHeight / height);
|
|
222
|
+
}
|
|
223
|
+
width *= scale;
|
|
224
|
+
height *= scale;
|
|
225
|
+
double x = (boxWidth - width) / 2 + offsetAlong(position, "left", "right", width, boxWidth);
|
|
226
|
+
double y = (boxHeight - height) / 2 + offsetAlong(position, "top", "bottom", height, boxHeight);
|
|
227
|
+
return {x, y, width, height};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
bool recyclingKeyClears(const std::string &before, const std::string &after) {
|
|
231
|
+
return !before.empty() && before != after;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
ImageTransition imageTransition(const folly::dynamic &transition) {
|
|
235
|
+
ImageTransition out;
|
|
236
|
+
if (transition.isNumber()) {
|
|
237
|
+
out.duration = transition.asDouble();
|
|
238
|
+
} else if (transition.isObject()) {
|
|
239
|
+
out.duration = numberIn(transition, "duration");
|
|
240
|
+
if (auto effect = textIn(transition, "effect"); !effect.empty()) {
|
|
241
|
+
out.effect = effect;
|
|
242
|
+
}
|
|
243
|
+
if (auto timing = textIn(transition, "timing"); !timing.empty()) {
|
|
244
|
+
out.timing = timing;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
std::optional<DecodedPixels> decodeBlurhash(const std::string &hash, int width, int height, double punch) {
|
|
251
|
+
if (hash.size() < 6 || width <= 0 || height <= 0) {
|
|
252
|
+
return std::nullopt;
|
|
253
|
+
}
|
|
254
|
+
bool ok = true;
|
|
255
|
+
int sizeFlag = decode83(hash, 0, 1, ok);
|
|
256
|
+
int numY = sizeFlag / 9 + 1, numX = sizeFlag % 9 + 1;
|
|
257
|
+
if (!ok || hash.size() != static_cast<size_t>(4 + 2 * numX * numY)) {
|
|
258
|
+
return std::nullopt;
|
|
259
|
+
}
|
|
260
|
+
double maxValue = (decode83(hash, 1, 2, ok) + 1) / 166.0 * punch;
|
|
261
|
+
std::vector<std::array<double, 3>> colors(static_cast<size_t>(numX * numY));
|
|
262
|
+
int dc = decode83(hash, 2, 6, ok);
|
|
263
|
+
colors[0] = {sRGBToLinear(dc >> 16), sRGBToLinear((dc >> 8) & 255), sRGBToLinear(dc & 255)};
|
|
264
|
+
for (size_t i = 1; i < colors.size(); i++) {
|
|
265
|
+
int ac = decode83(hash, 4 + i * 2, 6 + i * 2, ok);
|
|
266
|
+
colors[i] = {
|
|
267
|
+
signPow((ac / (19 * 19) - 9) / 9.0, 2) * maxValue,
|
|
268
|
+
signPow(((ac / 19) % 19 - 9) / 9.0, 2) * maxValue,
|
|
269
|
+
signPow((ac % 19 - 9) / 9.0, 2) * maxValue,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (!ok) {
|
|
273
|
+
return std::nullopt;
|
|
274
|
+
}
|
|
275
|
+
DecodedPixels out{width, height, std::vector<uint8_t>(static_cast<size_t>(width * height * 4))};
|
|
276
|
+
for (int y = 0; y < height; y++) {
|
|
277
|
+
for (int x = 0; x < width; x++) {
|
|
278
|
+
double r = 0, g = 0, b = 0;
|
|
279
|
+
for (int j = 0; j < numY; j++) {
|
|
280
|
+
for (int i = 0; i < numX; i++) {
|
|
281
|
+
double basis = std::cos(std::numbers::pi * x * i / width) * std::cos(std::numbers::pi * y * j / height);
|
|
282
|
+
const auto &color = colors[static_cast<size_t>(i + j * numX)];
|
|
283
|
+
r += color[0] * basis;
|
|
284
|
+
g += color[1] * basis;
|
|
285
|
+
b += color[2] * basis;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
auto *pixel = &out.rgba[static_cast<size_t>((y * width + x) * 4)];
|
|
289
|
+
pixel[0] = linearToSRGB(r);
|
|
290
|
+
pixel[1] = linearToSRGB(g);
|
|
291
|
+
pixel[2] = linearToSRGB(b);
|
|
292
|
+
pixel[3] = 255;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
std::optional<DecodedPixels> decodeThumbhash(const std::string &bytes) {
|
|
299
|
+
if (bytes.size() < 5) {
|
|
300
|
+
return std::nullopt;
|
|
301
|
+
}
|
|
302
|
+
auto at = [&](size_t i) -> int { return i < bytes.size() ? static_cast<unsigned char>(bytes[i]) : 0; };
|
|
303
|
+
int header24 = at(0) | (at(1) << 8) | (at(2) << 16);
|
|
304
|
+
int header16 = at(3) | (at(4) << 8);
|
|
305
|
+
double lDc = (header24 & 63) / 63.0;
|
|
306
|
+
double pDc = ((header24 >> 6) & 63) / 31.5 - 1;
|
|
307
|
+
double qDc = ((header24 >> 12) & 63) / 31.5 - 1;
|
|
308
|
+
double lScale = ((header24 >> 18) & 31) / 31.0;
|
|
309
|
+
bool hasAlpha = (header24 >> 23) != 0;
|
|
310
|
+
double pScale = ((header16 >> 3) & 63) / 63.0;
|
|
311
|
+
double qScale = ((header16 >> 9) & 63) / 63.0;
|
|
312
|
+
bool landscape = (header16 >> 15) != 0;
|
|
313
|
+
int lx = std::max(3, landscape ? (hasAlpha ? 5 : 7) : header16 & 7);
|
|
314
|
+
int ly = std::max(3, landscape ? header16 & 7 : (hasAlpha ? 5 : 7));
|
|
315
|
+
double aDc = hasAlpha ? (at(5) & 15) / 15.0 : 1;
|
|
316
|
+
double aScale = (at(5) >> 4) / 15.0;
|
|
317
|
+
size_t acStart = hasAlpha ? 6 : 5;
|
|
318
|
+
size_t acIndex = 0;
|
|
319
|
+
auto channel = [&](int nx, int ny, double scale) {
|
|
320
|
+
std::vector<double> ac;
|
|
321
|
+
for (int cy = 0; cy < ny; cy++) {
|
|
322
|
+
for (int cx = cy ? 0 : 1; cx * ny < nx * (ny - cy); cx++, acIndex++) {
|
|
323
|
+
ac.push_back((((at(acStart + (acIndex >> 1)) >> ((acIndex & 1) << 2)) & 15) / 7.5 - 1) * scale);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return ac;
|
|
327
|
+
};
|
|
328
|
+
auto lAc = channel(lx, ly, lScale);
|
|
329
|
+
auto pAc = channel(3, 3, pScale * 1.25);
|
|
330
|
+
auto qAc = channel(3, 3, qScale * 1.25);
|
|
331
|
+
auto aAc = hasAlpha ? channel(5, 5, aScale) : std::vector<double>{};
|
|
332
|
+
// The aspect ratio from the header's own component counts, before the minimum of 3.
|
|
333
|
+
int rx = landscape ? (hasAlpha ? 5 : 7) : at(3) & 7;
|
|
334
|
+
int ry = landscape ? at(3) & 7 : (hasAlpha ? 5 : 7);
|
|
335
|
+
double ratio = ry > 0 ? static_cast<double>(rx) / ry : 1;
|
|
336
|
+
int width = static_cast<int>(std::round(ratio > 1 ? 32 : 32 * ratio));
|
|
337
|
+
int height = static_cast<int>(std::round(ratio > 1 ? 32 / ratio : 32));
|
|
338
|
+
if (width <= 0 || height <= 0) {
|
|
339
|
+
return std::nullopt;
|
|
340
|
+
}
|
|
341
|
+
DecodedPixels out{width, height, std::vector<uint8_t>(static_cast<size_t>(width * height * 4))};
|
|
342
|
+
std::vector<double> fx(static_cast<size_t>(std::max(lx, hasAlpha ? 5 : 3)));
|
|
343
|
+
std::vector<double> fy(static_cast<size_t>(std::max(ly, hasAlpha ? 5 : 3)));
|
|
344
|
+
for (int y = 0; y < height; y++) {
|
|
345
|
+
for (int x = 0; x < width; x++) {
|
|
346
|
+
double l = lDc, p = pDc, q = qDc, a = aDc;
|
|
347
|
+
for (size_t cx = 0; cx < fx.size(); cx++) {
|
|
348
|
+
fx[cx] = std::cos(std::numbers::pi / width * (x + 0.5) * static_cast<double>(cx));
|
|
349
|
+
}
|
|
350
|
+
for (size_t cy = 0; cy < fy.size(); cy++) {
|
|
351
|
+
fy[cy] = std::cos(std::numbers::pi / height * (y + 0.5) * static_cast<double>(cy));
|
|
352
|
+
}
|
|
353
|
+
size_t j = 0;
|
|
354
|
+
for (int cy = 0; cy < ly; cy++) {
|
|
355
|
+
double fy2 = fy[static_cast<size_t>(cy)] * 2;
|
|
356
|
+
for (int cx = cy ? 0 : 1; cx * ly < lx * (ly - cy); cx++, j++) {
|
|
357
|
+
l += lAc[j] * fx[static_cast<size_t>(cx)] * fy2;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
j = 0;
|
|
361
|
+
for (int cy = 0; cy < 3; cy++) {
|
|
362
|
+
double fy2 = fy[static_cast<size_t>(cy)] * 2;
|
|
363
|
+
for (int cx = cy ? 0 : 1; cx < 3 - cy; cx++, j++) {
|
|
364
|
+
double f = fx[static_cast<size_t>(cx)] * fy2;
|
|
365
|
+
p += pAc[j] * f;
|
|
366
|
+
q += qAc[j] * f;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (hasAlpha) {
|
|
370
|
+
j = 0;
|
|
371
|
+
for (int cy = 0; cy < 5; cy++) {
|
|
372
|
+
double fy2 = fy[static_cast<size_t>(cy)] * 2;
|
|
373
|
+
for (int cx = cy ? 0 : 1; cx < 5 - cy; cx++, j++) {
|
|
374
|
+
a += aAc[j] * fx[static_cast<size_t>(cx)] * fy2;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
double b = l - 2.0 / 3 * p;
|
|
379
|
+
double r = (3 * l - b + q) / 2;
|
|
380
|
+
double g = r - q;
|
|
381
|
+
auto *pixel = &out.rgba[static_cast<size_t>((y * width + x) * 4)];
|
|
382
|
+
pixel[0] = static_cast<uint8_t>(std::max(0.0, 255 * std::min(1.0, r)));
|
|
383
|
+
pixel[1] = static_cast<uint8_t>(std::max(0.0, 255 * std::min(1.0, g)));
|
|
384
|
+
pixel[2] = static_cast<uint8_t>(std::max(0.0, 255 * std::min(1.0, b)));
|
|
385
|
+
pixel[3] = static_cast<uint8_t>(std::max(0.0, 255 * std::min(1.0, a)));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
std::optional<DecodedPixels> decodeHashUri(const std::string &uri, double width, double height) {
|
|
392
|
+
if (uri.rfind("blurhash:/", 0) == 0) {
|
|
393
|
+
auto rest = uri.substr(10);
|
|
394
|
+
auto hash = percentDecoded(rest.substr(0, rest.find('/')));
|
|
395
|
+
// expo-image decodes a blurhash at its width and height, 16 by default.
|
|
396
|
+
int w = width > 0 ? static_cast<int>(width) : 16, h = height > 0 ? static_cast<int>(height) : 16;
|
|
397
|
+
return decodeBlurhash(hash, std::min(w, 128), std::min(h, 128));
|
|
398
|
+
}
|
|
399
|
+
if (uri.rfind("thumbhash:/", 0) == 0) {
|
|
400
|
+
auto text = percentDecoded(uri.substr(11));
|
|
401
|
+
std::replace(text.begin(), text.end(), '\\', '/');
|
|
402
|
+
auto bytes = base64Decode(text);
|
|
403
|
+
return bytes ? decodeThumbhash(*bytes) : std::nullopt;
|
|
404
|
+
}
|
|
405
|
+
return std::nullopt;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
ImageCache::ImageCache(std::string folder, size_t memoryLimit) : folder_(std::move(folder)), limit_(memoryLimit) {}
|
|
409
|
+
|
|
410
|
+
std::string ImageCache::diskPath(const std::string &key) const {
|
|
411
|
+
char name[40];
|
|
412
|
+
std::snprintf(name, sizeof name, "%016llx%016llx", static_cast<unsigned long long>(fnv1a(key, 14695981039346656037ULL)),
|
|
413
|
+
static_cast<unsigned long long>(fnv1a(key, 1099511628211ULL)));
|
|
414
|
+
return folder_ + "/" + name;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
std::optional<std::pair<std::string, ImageCache::Hit>> ImageCache::find(const std::string &key, const std::string &policy) {
|
|
418
|
+
if (policy == "memory" || policy == "memory-disk") {
|
|
419
|
+
std::lock_guard lock(mutex_);
|
|
420
|
+
if (auto found = byKey_.find(key); found != byKey_.end()) {
|
|
421
|
+
recent_.splice(recent_.begin(), recent_, found->second);
|
|
422
|
+
return std::pair{found->second->second, Hit::Memory};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (policy == "disk" || policy == "memory-disk") {
|
|
426
|
+
auto path = diskPath(key);
|
|
427
|
+
std::error_code ignored;
|
|
428
|
+
if (std::filesystem::exists(path, ignored)) {
|
|
429
|
+
auto bytes = contentsOf(path);
|
|
430
|
+
if (policy == "memory-disk") {
|
|
431
|
+
remember(key, bytes);
|
|
432
|
+
}
|
|
433
|
+
return std::pair{std::move(bytes), Hit::Disk};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return std::nullopt;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
void ImageCache::remember(const std::string &key, const std::string &bytes) {
|
|
440
|
+
std::lock_guard lock(mutex_);
|
|
441
|
+
if (auto found = byKey_.find(key); found != byKey_.end()) {
|
|
442
|
+
bytes_ -= found->second->second.size();
|
|
443
|
+
recent_.erase(found->second);
|
|
444
|
+
byKey_.erase(found);
|
|
445
|
+
}
|
|
446
|
+
if (bytes.size() > limit_) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
recent_.emplace_front(key, bytes);
|
|
450
|
+
byKey_[key] = recent_.begin();
|
|
451
|
+
bytes_ += bytes.size();
|
|
452
|
+
while (bytes_ > limit_ && !recent_.empty()) {
|
|
453
|
+
bytes_ -= recent_.back().second.size();
|
|
454
|
+
byKey_.erase(recent_.back().first);
|
|
455
|
+
recent_.pop_back();
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
void ImageCache::store(const std::string &key, const std::string &bytes, const std::string &policy) {
|
|
460
|
+
if (policy == "memory" || policy == "memory-disk") {
|
|
461
|
+
remember(key, bytes);
|
|
462
|
+
}
|
|
463
|
+
if (policy == "disk" || policy == "memory-disk") {
|
|
464
|
+
std::error_code ignored;
|
|
465
|
+
std::filesystem::create_directories(folder_, ignored);
|
|
466
|
+
// A write that fails publishes nothing, and leaves an entry already there as it was.
|
|
467
|
+
replaceFile(diskPath(key), bytes);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
void ImageCache::clearMemory() {
|
|
472
|
+
std::lock_guard lock(mutex_);
|
|
473
|
+
recent_.clear();
|
|
474
|
+
byKey_.clear();
|
|
475
|
+
bytes_ = 0;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
bool ImageCache::clearDisk() {
|
|
479
|
+
std::error_code error;
|
|
480
|
+
std::filesystem::remove_all(folder_, error);
|
|
481
|
+
return !error;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
size_t ImageCache::memoryBytes() const {
|
|
485
|
+
std::lock_guard lock(mutex_);
|
|
486
|
+
return bytes_;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const char *cacheTypeName(ImageCache::Hit hit) {
|
|
490
|
+
switch (hit) {
|
|
491
|
+
case ImageCache::Hit::Memory:
|
|
492
|
+
return "memory";
|
|
493
|
+
case ImageCache::Hit::Disk:
|
|
494
|
+
return "disk";
|
|
495
|
+
default:
|
|
496
|
+
return "none";
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
void loadImageBytes(
|
|
501
|
+
const std::shared_ptr<Http> &http,
|
|
502
|
+
const std::shared_ptr<ImageCache> &cache,
|
|
503
|
+
const FileAccess *files,
|
|
504
|
+
const ImageSourceRef &source,
|
|
505
|
+
const std::string &policy,
|
|
506
|
+
std::function<void(std::string, ImageCache::Hit, const std::string &)> done) {
|
|
507
|
+
std::string url;
|
|
508
|
+
std::shared_ptr<FileAccess::Use> use;
|
|
509
|
+
bool remote = source.uri.rfind("http://", 0) == 0 || source.uri.rfind("https://", 0) == 0;
|
|
510
|
+
if (source.uri.starts_with('/') || isFileScheme(source.uri)) {
|
|
511
|
+
try {
|
|
512
|
+
auto readable = readableSource(files, source.uri);
|
|
513
|
+
url = readable.uri;
|
|
514
|
+
use = readable.use;
|
|
515
|
+
} catch (const FileRefused &refused) {
|
|
516
|
+
done("", ImageCache::Hit::None, refused.what());
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
} else if (remote || source.uri.rfind("data:", 0) == 0) {
|
|
520
|
+
url = source.uri;
|
|
521
|
+
} else {
|
|
522
|
+
done("", ImageCache::Hit::None, source.uri + " is not a file, data or http(s) uri");
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
auto key = source.cacheKey.empty() ? source.uri : source.cacheKey;
|
|
526
|
+
if (remote && cache) {
|
|
527
|
+
if (auto found = cache->find(key, policy)) {
|
|
528
|
+
done(std::move(found->first), found->second, "");
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
HttpRequest request{.url = url};
|
|
533
|
+
for (const auto &[name, value] : source.headers) {
|
|
534
|
+
request.headers.emplace_back(name, value);
|
|
535
|
+
}
|
|
536
|
+
fetchBytes(http, std::move(request), [cache, key, policy, remote, done, use](std::string body, const std::string &error) {
|
|
537
|
+
if (!error.empty()) {
|
|
538
|
+
done("", ImageCache::Hit::None, error);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
if (remote && cache) {
|
|
542
|
+
cache->store(key, body, policy);
|
|
543
|
+
}
|
|
544
|
+
done(std::move(body), ImageCache::Hit::None, "");
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
} // namespace ngn
|