Skip to content

Commit 2bf3418

Browse files
committed
can configure max kb size for image compressor
1 parent 35ad553 commit 2bf3418

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

ghcjs/delivery-calculator/src/App/Jsm.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fetchBlobUris st = do
1313
vars <-
1414
forM blobUris $ \uri -> do
1515
var <- newEmptyMVar
16-
Jsm.fetchUrlAsRfc2397 uri
16+
Jsm.fetchUrlAsRfc2397 (Just 50000) uri
1717
$ liftIO
1818
. putMVar var
1919
. fmap (uri,)

ghcjs/miso-functora/js/main.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ export async function insertStorage(key, value) {
3030
return await Preferences.set({ key: key, value: value });
3131
}
3232

33-
export async function compressImage(quality, prevImage) {
33+
export async function compressImage(maxSizeKb, prevImage) {
34+
let opts = { quality: 1 };
35+
if (maxSizeKb) {
36+
opts = {
37+
quality: Math.min(1, maxSizeKb / prevImage.size),
38+
maxWidth: 768,
39+
maxHeight: 768,
40+
};
41+
}
3442
const nextImage = await new Promise((resolve, reject) => {
3543
new Compressor(prevImage, {
36-
quality: quality,
3744
mimeType: "image/jpeg",
3845
success: resolve,
3946
error: reject,
47+
...opts,
4048
});
4149
});
4250
return nextImage;
@@ -64,7 +72,7 @@ export async function selectDataUrl(value, opfsName = null) {
6472
const { buffer: u8a, typeFull: mime } = dataUriToBuffer(value);
6573
let blob = new Blob([u8a], { type: mime });
6674
if (mime.startsWith("image")) {
67-
blob = await compressImage(1, blob);
75+
blob = await compressImage(null, blob);
6876
}
6977
if (opfsName) {
7078
await opfsWrite(value, opfsName);
@@ -82,7 +90,6 @@ export async function opfsWrite(value, opfsName) {
8290
const stream = await handle.createWritable();
8391
await stream.write(value);
8492
await stream.close();
85-
console.log("OPFS write success:", handle, opfsName);
8693
} catch (e) {
8794
alert("OPFS write failure: " + e.toString() + " file: " + opfsName);
8895
}
@@ -96,7 +103,6 @@ export async function opfsRead(opfsName) {
96103
const file = await handle.getFile();
97104
const uri = await file.text();
98105
const res = await selectDataUrl(uri);
99-
console.log("OPFS read success:", res, opfsName);
100106
return res;
101107
} catch (e) {
102108
alert("OPFS read failure: " + e.toString() + " file: " + opfsName);
@@ -111,7 +117,6 @@ export async function opfsList() {
111117
for await (let opfsName of root.keys()) {
112118
res.push(opfsName);
113119
}
114-
console.log("OPFS list success:", res, opfsName);
115120
return res;
116121
} catch (e) {
117122
alert("OPFS list failure: " + e.toString() + " file: " + opfsName);
@@ -195,10 +200,10 @@ export function isNativePlatform() {
195200
return Capacitor.isNativePlatform();
196201
}
197202

198-
export async function fetchUrlAsRfc2397(url) {
203+
export async function fetchUrlAsRfc2397(maxSizeKb, url) {
199204
const imgResp = await fetch(url);
200205
const imgBlob = await imgResp.blob();
201-
const imgComp = await compressImage(0.2, imgBlob);
206+
const imgComp = await compressImage(maxSizeKb, imgBlob);
202207
const rfc2397 = await new Promise((resolve, reject) => {
203208
var fr = new FileReader();
204209
fr.onload = () => {

ghcjs/miso-functora/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/miso-functora/src/Functora/Miso/Jsm/Generic.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,12 @@ saveFileThen onSuccess name mime bs = do
250250
Nothing -> pure ()
251251
Just str -> onSuccess str
252252

253-
fetchUrlAsRfc2397 :: Unicode -> (Maybe ByteString -> JSM ()) -> JSM ()
254-
fetchUrlAsRfc2397 url after = do
253+
fetchUrlAsRfc2397 ::
254+
Maybe Int ->
255+
Unicode ->
256+
(Maybe ByteString -> JSM ()) ->
257+
JSM ()
258+
fetchUrlAsRfc2397 maxSizeKb url after = do
255259
success <- JS.function $ \_ _ ->
256260
handleAny
257261
( \e -> do
@@ -276,5 +280,10 @@ fetchUrlAsRfc2397 url after = do
276280
alert $ "Failure, " <> inspect msg <> "!"
277281
after Nothing
278282
pkg <- getPkg
279-
prom <- pkg ^. JS.jsf ("fetchUrlAsRfc2397" :: Unicode) ([url] :: [Unicode])
283+
argv <-
284+
sequence
285+
[ JS.toJSVal maxSizeKb,
286+
JS.toJSVal url
287+
]
288+
prom <- pkg ^. JS.jsf ("fetchUrlAsRfc2397" :: Unicode) (argv :: [JS.JSVal])
280289
void $ prom ^. JS.js2 @Unicode "then" success failure

0 commit comments

Comments
 (0)