bajo-extra 2.2.1 → 2.4.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.
@@ -18,5 +18,7 @@
18
18
  "decryption type": "decryption type",
19
19
  "fetching%s": "Fetching %s",
20
20
  "peerError%s": "Peer error: %s",
21
- "disabled%s": "%s is disabled"
21
+ "disabled%s": "%s is disabled",
22
+ "tnUnsupportedFormat%s%s": "Unsupported thumbnail format in '%s', available: %s",
23
+ "tnInvalidSize%s%s": "Unsupported thumbnail size '%s' for '%s'"
22
24
  }
@@ -18,5 +18,7 @@
18
18
  "decryption type": "Tipe dekripsi",
19
19
  "fetching%s": "Fetching %s",
20
20
  "peerError%s": "Kesalahan dari peer: %s",
21
- "disabled%s": "%s dinonaktifkan"
21
+ "disabled%s": "%s dinonaktifkan",
22
+ "tnUnsupportedFormat%s%s": "Format thumbnail tidak didukung di '%s', tersedia: %s",
23
+ "tnInvalidSize%s%s": "Ukuran thumbnail '%s' tidak didukung, tersedia: '%s'"
22
24
  }
package/index.js CHANGED
@@ -3,6 +3,7 @@ import { createGzip, createGunzip } from 'zlib'
3
3
  import path from 'path'
4
4
  import { Readable } from 'stream'
5
5
  import numbro from 'numbro'
6
+ import sharp from 'sharp'
6
7
 
7
8
  async function fetching ({ url, opts, bulk, spin }) {
8
9
  const { setImmediate, print } = this.app.bajo
@@ -129,6 +130,10 @@ async function factory (pkgName) {
129
130
  autoSelectFamilyAttemptTimeout: 1000,
130
131
  autoSelectFamily: true
131
132
  }
133
+ },
134
+ thumbnail: {
135
+ inputFormats: ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.avif', '.tiff', '.svg'],
136
+ outputFormats: ['.png']
132
137
  }
133
138
  }
134
139
  }
@@ -362,6 +367,10 @@ async function factory (pkgName) {
362
367
  return /^[a-f0-9]{32}$/i.test(text)
363
368
  }
364
369
 
370
+ isSha256 = (text) => {
371
+ return /^[a-f0-9]{64}$/i.test(text)
372
+ }
373
+
365
374
  encrypt = async (text, { type = 'short', subType = 'qr' } = {}) => {
366
375
  const { importPkg } = this.app.bajo
367
376
  const { ShortCrypt } = await importPkg('bajoExtra:short-crypt')
@@ -395,6 +404,59 @@ async function factory (pkgName) {
395
404
  if (!alpha) return num
396
405
  return String.fromCharCode(96 + num)
397
406
  }
407
+
408
+ thumbnailSizes = name => {
409
+ switch (name) {
410
+ case 's': return [36, 36]
411
+ case 'm': return [100, 100]
412
+ case 'l': return [250, 250]
413
+ default: {
414
+ const [w, h] = name.split('x').map(s => parseInt(s))
415
+ if (!w || !h) return [0, 0]
416
+ return [w, h]
417
+ }
418
+ }
419
+ }
420
+
421
+ createThumbnail = async (file, options = {}) => {
422
+ const { fs } = this.app.lib
423
+ const { isString } = this.app.lib._
424
+ let {
425
+ dir = path.dirname(file),
426
+ silent = true,
427
+ size = this.config.thumbnail.sizes,
428
+ format = this.config.thumbnail.outputFormats,
429
+ opts = {}
430
+ } = options
431
+ if (isString(size)) size = [size]
432
+ if (isString(format)) format = [format]
433
+ const ext = path.extname(file)
434
+
435
+ if (!this.config.thumbnail.inputFormats.includes(ext)) {
436
+ if (silent) return
437
+ throw this.error('tnUnsupportedFormat%s%s', file, this.config.thumbnail.inputFormats)
438
+ }
439
+ const base = path.basename(file, ext)
440
+ fs.ensureDirSync(dir)
441
+ for (const s of size) {
442
+ const [w, h] = this.thumbnailSizes(s)
443
+ if (w === 0 || h === 0) {
444
+ if (silent) continue
445
+ throw this.error('tnInvalidSize%s%s', file, [...size, '<width>x<height>'])
446
+ }
447
+ for (const to of format) {
448
+ const dest = `${dir}/${base}-${s}${to}`
449
+ try {
450
+ await sharp(file)
451
+ .resize(w, h, opts)
452
+ .toFile(dest)
453
+ } catch (err) {
454
+ if (silent) continue
455
+ throw err
456
+ }
457
+ }
458
+ }
459
+ }
398
460
  }
399
461
 
400
462
  return BajoExtra
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "2.2.1",
3
+ "version": "2.4.0",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -38,6 +38,7 @@
38
38
  "fast-xml-parser": "^5.3.3",
39
39
  "numbro": "^2.5.0",
40
40
  "performant-array-to-tree": "^1.11.0",
41
+ "sharp": "^0.34.5",
41
42
  "short-crypt": "^4.0.0",
42
43
  "undici": "^7.16.0"
43
44
  },
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-05-02
4
+
5
+ - [2.4.0] Add ```sharp``` as library
6
+ - [2.4.0] Add ```thumbnailSizes()```
7
+ - [2.4.0] Add ```createThumbnail()```
8
+
9
+ ## 2026-04-25
10
+
11
+ - [2.3.0] Add ```isSha256()```
12
+
3
13
  ## 2026-01-16
4
14
 
5
15
  - [2.2.1] Bug fix on ```fetchUrl()``` for url with query string