@startupjs-ui/file-input 0.1.5 → 0.1.8

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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.1.8](https://github.com/startupjs/startupjs-ui/compare/v0.1.7...v0.1.8) (2026-01-08)
7
+
8
+ **Note:** Version bump only for package @startupjs-ui/file-input
9
+
10
+
11
+
12
+
13
+
6
14
  ## [0.1.5](https://github.com/startupjs/startupjs-ui/compare/v0.1.4...v0.1.5) (2025-12-29)
7
15
 
8
16
  **Note:** Version bump only for package @startupjs-ui/file-input
package/files.plugin.js CHANGED
@@ -3,7 +3,8 @@ import { createPlugin } from 'startupjs/registry'
3
3
  import busboy from 'busboy'
4
4
  import sharp from 'sharp'
5
5
  import { DELETE_FILE_URL, GET_FILE_URL, getDeleteFileUrl, getFileUrl, getUploadFileUrl, UPLOAD_SINGLE_FILE_URL } from './constants.js'
6
- import { deleteFile, getDefaultStorageType, getFileBlob, getFileSize, saveFileBlob } from './providers/index.js'
6
+ import { deleteFile, getDefaultStorageType, getFileBlob, getFileSize } from './providers/index.js'
7
+ import { uploadBuffer } from './server/index.js'
7
8
 
8
9
  export default createPlugin({
9
10
  name: 'files',
@@ -217,51 +218,19 @@ export default createPlugin({
217
218
  stream.on('data', data => buffers.push(data))
218
219
 
219
220
  stream.on('end', async () => {
220
- blob = Buffer.concat(buffers)
221
- meta = { filename, mimeType, encoding, storageType } // Update meta here to ensure it includes modifications for images
222
-
221
+ const blob = Buffer.concat(buffers)
222
+ meta = { filename, mimeType, encoding, storageType }
223
223
  if (!blob) return res.status(500).send('No file was uploaded')
224
224
 
225
225
  // extract extension from filename
226
226
  console.log('meta.filename', meta.filename)
227
227
  const extension = meta.filename?.match(/\.([^.]+)$/)?.[1]
228
228
  if (extension) meta.extension = extension
229
- const create = !fileId
230
- if (!fileId) fileId = $.id()
231
- // try to save file to sqlite first to do an early exit if it fails
232
229
  try {
233
- await saveFileBlob(storageType, fileId, blob)
230
+ fileId = await uploadBuffer(blob, { fileId, meta })
234
231
  } catch (err) {
235
232
  console.error(err)
236
- return res.status(500).send('Error saving file')
237
- }
238
- if (create) {
239
- const doc = { id: fileId, ...meta }
240
- // if some of the meta fields were undefined, remove them from the doc
241
- for (const key in meta) {
242
- if (meta[key] == null) delete doc[key]
243
- }
244
- await $.files.addNew(doc)
245
- } else {
246
- const $file = await sub($.files[fileId])
247
-
248
- // when changing storageType we should delete the file from the old storageType
249
- const oldStorageType = $file.storageType.get()
250
- if (oldStorageType !== meta.storageType) {
251
- try {
252
- await deleteFile(oldStorageType, fileId)
253
- } catch (err) {
254
- console.error(err)
255
- return res.status(500).send(`Error deleting file from old storageType ${oldStorageType}`)
256
- }
257
- }
258
-
259
- const doc = { ...$file.get(), ...meta, updatedAt: Date.now() }
260
- // if some of the meta fields were undefined, remove them from the doc
261
- for (const key in meta) {
262
- if (meta[key] == null) delete doc[key]
263
- }
264
- await $file.set(doc)
233
+ return res.status(500).send(err.message)
265
234
  }
266
235
  console.log(`Uploaded file to ${storageType}`, fileId)
267
236
  res.json({ fileId })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startupjs-ui/file-input",
3
- "version": "0.1.5",
3
+ "version": "0.1.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -9,6 +9,7 @@
9
9
  "type": "module",
10
10
  "exports": {
11
11
  ".": "./index.tsx",
12
+ "./server": "./server/index.js",
12
13
  "./constants": "./constants.js",
13
14
  "./deleteFile": "./deleteFile.ts",
14
15
  "./uploadFile": "./uploadFile.ts",
@@ -41,5 +42,5 @@
41
42
  "optional": true
42
43
  }
43
44
  },
44
- "gitHead": "1b90893dc24a9b3ffde1284c58996b42e98913c6"
45
+ "gitHead": "377cf459b4b85dc030aeb78621f7c28a0885c7f6"
45
46
  }
@@ -0,0 +1 @@
1
+ export { default as uploadBuffer } from './uploadBuffer.js'
@@ -0,0 +1,54 @@
1
+ import { $, sub } from 'startupjs'
2
+ import { deleteFile, getDefaultStorageType, saveFileBlob } from '../providers/index.js'
3
+
4
+ export default async function uploadBuffer (buff, options = {}) {
5
+ let { fileId, meta = {} } = options
6
+
7
+ let storageType = meta.storageType
8
+ try {
9
+ storageType ??= await getDefaultStorageType()
10
+ } catch (err) {
11
+ console.error(err)
12
+ throw new Error('Error getting default storage type')
13
+ }
14
+
15
+ const create = !fileId
16
+ if (!fileId) fileId = $.id()
17
+
18
+ // try to save file to sqlite first to do an early exit if it fails
19
+ try {
20
+ await saveFileBlob(storageType, fileId, buff)
21
+ } catch (err) {
22
+ console.error(err)
23
+ throw new Error('Error saving file')
24
+ }
25
+ if (create) {
26
+ const doc = { id: fileId, ...meta, storageType }
27
+ // if some of the meta fields were undefined, remove them from the doc
28
+ for (const key in meta) {
29
+ if (meta[key] == null) delete doc[key]
30
+ }
31
+ await $.files.addNew(doc)
32
+ } else {
33
+ const $file = await sub($.files[fileId])
34
+
35
+ // when changing storageType we should delete the file from the old storageType
36
+ const oldStorageType = $file.storageType.get()
37
+ if (oldStorageType !== storageType) {
38
+ try {
39
+ await deleteFile(oldStorageType, fileId)
40
+ } catch (err) {
41
+ console.error(err)
42
+ throw new Error(`Error deleting file from old storageType ${oldStorageType}`)
43
+ }
44
+ }
45
+
46
+ const doc = { ...$file.get(), ...meta, storageType, updatedAt: Date.now() }
47
+ // if some of the meta fields were undefined, remove them from the doc
48
+ for (const key in meta) {
49
+ if (meta[key] == null) delete doc[key]
50
+ }
51
+ await $file.set(doc)
52
+ }
53
+ return fileId
54
+ }