Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from datetime import datetime, timezone

from pydantic import BaseModel, Field

from lnbits.db import FilterModel
from pydantic import BaseModel, Field


class CreateInventory(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "lnbits-inventory"
name = "inventory"
version = "0.0.0"
requires-python = ">=3.10,<3.13"
description = "Shared Inventory extension"
Expand Down
36 changes: 24 additions & 12 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,26 +555,38 @@ window.app = Vue.createApp({
}
},
async updateItem(data) {
const newPhotos = this.itemDialog.gallery.filter(p => p.isNew && p.file)
let newAssetIds = []
const filesToUpload = this.itemDialog.gallery
.filter(
p =>
(p.isNew && p.file) ||
(!p.isNew && p.assetId && isBase64String(p.assetId))
)
.map(p => {
if (p.isNew && p.file) return p.file
if (!p.isNew && p.assetId && isBase64String(p.assetId))
return base64ToFile(p.assetId)
return null
})
.filter(Boolean)

if (newPhotos.length > 0) {
let uploadedAssetIds = []
if (filesToUpload.length > 0) {
try {
newAssetIds = await Promise.all(
newPhotos.map(p => this.uploadPhoto(p.file))
uploadedAssetIds = await Promise.all(
filesToUpload.map(file => this.uploadPhoto(file))
)
} catch (error) {
LNbits.utils.notifyError('Failed to upload new photos')
LNbits.utils.notifyError('Failed to upload photos')
return
}
}

const finalIds = [
...this.itemDialog.gallery
.filter(p => !p.isNew && p.assetId)
.map(p => p.assetId),
...newAssetIds
]
// Asset IDs from gallery that are not new and not base64
const existingAssetIds = this.itemDialog.gallery
.filter(p => !p.isNew && p.assetId && !isBase64String(p.assetId))
.map(p => p.assetId)

const finalIds = [...existingAssetIds, ...uploadedAssetIds]

data.images = toCsv(finalIds)

Expand Down
12 changes: 12 additions & 0 deletions static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ function fileToBase64(file) {
})
}

function base64ToFile(base64String, filename) {
const arr = base64String.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], filename, {type: mime})
}

function isBase64String(str) {
if (typeof str !== 'string') return false
return str.includes('data:') && str.includes('base64')
Expand Down
77 changes: 46 additions & 31 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading