@seedprotocol/sdk 0.3.0 → 0.3.1
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/dist/{ArweaveClient-CmJYeuq1.js → ArweaveClient-C0g0es2v.js} +2 -2
- package/dist/{ArweaveClient-EXXrXUzz.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
- package/dist/{ArweaveClient-EXXrXUzz.js → ArweaveClient-CzkBgiZY.js} +2 -2
- package/dist/{ArweaveClient-CmJYeuq1.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
- package/dist/{Db-CESKJH-G.js → Db-C4oog-0n.js} +3 -3
- package/dist/{Db-CESKJH-G.js.map → Db-C4oog-0n.js.map} +1 -1
- package/dist/{Db-DiTPSbQ8.js → Db-VPouegkr.js} +3 -3
- package/dist/{Db-DiTPSbQ8.js.map → Db-VPouegkr.js.map} +1 -1
- package/dist/{EasClient-BesHK2bO.js → EasClient-B9a3NsTi.js} +2 -2
- package/dist/{EasClient-BesHK2bO.js.map → EasClient-B9a3NsTi.js.map} +1 -1
- package/dist/{EasClient-Cop6WtWl.js → EasClient-DQ93i2ci.js} +2 -2
- package/dist/{EasClient-Cop6WtWl.js.map → EasClient-DQ93i2ci.js.map} +1 -1
- package/dist/{FileManager-DTzkncLh.js → FileManager-_ErxVuwb.js} +8 -5
- package/dist/FileManager-_ErxVuwb.js.map +1 -0
- package/dist/{Item-B5Tz-Y7t.js → Item-D9HFPxsK.js} +3 -3
- package/dist/{Item-B5Tz-Y7t.js.map → Item-D9HFPxsK.js.map} +1 -1
- package/dist/{ItemProperty-DBV0Iib_.js → ItemProperty-BdtvKrEy.js} +3 -3
- package/dist/{ItemProperty-DBV0Iib_.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
- package/dist/{QueryClient-C7a0NHCt.js → QueryClient-Bmnv_moN.js} +2 -2
- package/dist/{QueryClient-C7a0NHCt.js.map → QueryClient-Bmnv_moN.js.map} +1 -1
- package/dist/{QueryClient-BnwbHtqe.js → QueryClient-hzOtFe99.js} +2 -2
- package/dist/{QueryClient-BnwbHtqe.js.map → QueryClient-hzOtFe99.js.map} +1 -1
- package/dist/bin.js +5 -5
- package/dist/bin.js.map +1 -1
- package/dist/{constants-CWg2B3Au.js → constants-Bhl5dZpN.js} +4 -1
- package/dist/constants-Bhl5dZpN.js.map +1 -0
- package/dist/{index-CQ9FS10O.js → index-BAtjd9W-.js} +3 -3
- package/dist/index-BAtjd9W-.js.map +1 -0
- package/dist/{index-BHGJ-Es9.js → index-BSR5hv7Q.js} +59 -27
- package/dist/index-BSR5hv7Q.js.map +1 -0
- package/dist/main.js +2 -2
- package/dist/{seed.schema.config-DDzfQguf.js → seed.schema.config-BxJYabim.js} +3 -3
- package/dist/{seed.schema.config-DDzfQguf.js.map → seed.schema.config-BxJYabim.js.map} +1 -1
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/ArweaveClient.ts +13 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/BaseArweaveClient.ts +13 -0
- package/dist/src/BaseClientManager.ts +35 -0
- package/dist/src/BaseDb.ts +35 -0
- package/dist/src/BaseEasClient.ts +14 -0
- package/dist/src/BaseFileManager.ts +60 -0
- package/dist/src/BaseItem.ts +413 -0
- package/dist/src/BaseItemProperty.ts +416 -0
- package/dist/src/BaseQueryClient.ts +13 -0
- package/dist/src/ClientManager.ts +63 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/Db.ts +82 -0
- package/dist/src/EasClient.ts +13 -0
- package/dist/src/FileDownloader.ts +63 -0
- package/dist/src/FileManager.ts +59 -0
- package/dist/src/IDb.ts +7 -0
- package/dist/src/IEasClient.ts +2 -0
- package/dist/src/IItem.ts +26 -0
- package/dist/src/IItemProperty.ts +28 -0
- package/dist/src/IQueryClient.ts +11 -0
- package/dist/src/ImageResizer.ts +81 -0
- package/dist/src/Item.ts +12 -0
- package/dist/src/ItemProperty.ts +9 -0
- package/dist/src/MetadataSchema.ts +28 -0
- package/dist/src/ModelSchema.ts +46 -0
- package/dist/src/ModelUidSchema.ts +16 -0
- package/dist/src/PropertyUidSchema.ts +16 -0
- package/dist/src/QueryClient.ts +26 -0
- package/dist/src/Schema.ts +17 -0
- package/dist/src/SeedProtocolSdk.d.ts +19 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +295 -0
- package/dist/src/addModelsToDb.ts +152 -0
- package/dist/src/allItems.ts +31 -0
- package/dist/src/analyzeInput.ts +144 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +57 -0
- package/dist/src/clientManagerMachine.ts +59 -0
- package/dist/src/configureFs.ts +98 -0
- package/dist/src/connectToDb.ts +41 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +125 -0
- package/dist/src/content-hash.ts +30 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createMetadata.ts +77 -0
- package/dist/src/createNewItem.ts +61 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeed.ts +31 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/dbMachine.ts +182 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +208 -0
- package/dist/src/drizzle.ts +78 -0
- package/dist/src/eas.ts +40 -0
- package/dist/src/environment.ts +35 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +94 -0
- package/dist/src/fetchDbData.ts +19 -0
- package/dist/src/fetchRelatedItems.ts +184 -0
- package/dist/src/fetchSeeds.ts +45 -0
- package/dist/src/fetchVersions.ts +49 -0
- package/dist/src/files.ts +81 -0
- package/dist/src/filesDownload.ts +326 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/getExistingItem.ts +60 -0
- package/dist/src/getItem.ts +32 -0
- package/dist/src/getItemData.ts +99 -0
- package/dist/src/getItemProperties.ts +56 -0
- package/dist/src/getItemProperty.ts +34 -0
- package/dist/src/getItems.ts +70 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +89 -0
- package/dist/src/getPropertyData.ts +50 -0
- package/dist/src/getPublishPayload.ts +355 -0
- package/dist/src/getPublishUploads.ts +207 -0
- package/dist/src/getRelationValueData.ts +27 -0
- package/dist/src/getSchemaForItemProperty.ts +68 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSchemaUidForModel.ts +32 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getSegmentedItemProperties.ts +65 -0
- package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
- package/dist/src/getVersionData.ts +46 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +280 -0
- package/dist/src/gql.ts +118 -0
- package/dist/src/graphql.ts +3209 -0
- package/dist/src/helpers.ts +273 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +270 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/imageResize.ts +507 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +4 -0
- package/dist/src/initialize.ts +124 -0
- package/dist/src/internalMachine.ts +241 -0
- package/dist/src/itemMachineAll.ts +157 -0
- package/dist/src/itemMachineSingle.ts +158 -0
- package/dist/src/loadAppDb.ts +44 -0
- package/dist/src/machines.ts +66 -0
- package/dist/src/metadataLatest.ts +34 -0
- package/dist/src/migrate.ts +57 -0
- package/dist/src/model.ts +72 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +41 -0
- package/dist/src/prepareDb.ts +29 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +74 -0
- package/dist/src/property.ts +156 -0
- package/dist/src/propertyMachine.ts +202 -0
- package/dist/src/publish.ts +28 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/reload.ts +33 -0
- package/dist/src/request.ts +45 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +367 -0
- package/dist/src/resolveRemoteStorage.ts +88 -0
- package/dist/src/saveAppState.ts +50 -0
- package/dist/src/saveConfig.ts +106 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveImageSrc.ts +242 -0
- package/dist/src/saveItemStorage.ts +156 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/savePublishService.ts +30 -0
- package/dist/src/saveRelation.ts +112 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +10 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +365 -0
- package/dist/src/setAddresses.ts +16 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +628 -0
- package/dist/src/trash.ts +29 -0
- package/dist/src/ts-to-proto.ts +101 -0
- package/dist/src/types.ts +12 -0
- package/dist/src/updateItemPropertyValue.ts +243 -0
- package/dist/src/updateMachineContext.ts +21 -0
- package/dist/src/updateMetadata.ts +59 -0
- package/dist/src/upload.ts +86 -0
- package/dist/src/validate.ts +45 -0
- package/dist/src/validateInput.ts +43 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/versionData.ts +24 -0
- package/dist/src/waitForDb.ts +25 -0
- package/dist/src/waitForFiles.ts +41 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/types/scripts/bin.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
- package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/FileManager-DTzkncLh.js.map +0 -1
- package/dist/constants-CWg2B3Au.js.map +0 -1
- package/dist/index-BHGJ-Es9.js.map +0 -1
- package/dist/index-CQ9FS10O.js.map +0 -1
- package/dist/package.json +0 -189
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { itemMachineSingle } from '@/Item/service/itemMachineSingle'
|
|
3
|
+
import { sql } from 'drizzle-orm'
|
|
4
|
+
import { escapeSqliteString } from '@/helpers/db'
|
|
5
|
+
import { generateId } from '@/helpers'
|
|
6
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
7
|
+
|
|
8
|
+
const relatedSnakeCaseToCamelCase = (snakeCase: string): string => {
|
|
9
|
+
let camelCasePropertyName = snakeCase
|
|
10
|
+
|
|
11
|
+
camelCasePropertyName = camelCasePropertyName.replace(/_(id|ids)$/, '')
|
|
12
|
+
|
|
13
|
+
return camelCasePropertyName
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/[-_][a-z]/g, (group) => group.slice(-1).toUpperCase())
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const saveDataToDb = fromCallback<EventObject, typeof itemMachineSingle>(
|
|
19
|
+
({ sendBack, input: { context } }) => {
|
|
20
|
+
const {
|
|
21
|
+
modelName,
|
|
22
|
+
modelTableName,
|
|
23
|
+
versionUid,
|
|
24
|
+
versionLocalId,
|
|
25
|
+
seedLocalId,
|
|
26
|
+
propertiesBySchemaUid,
|
|
27
|
+
} = context
|
|
28
|
+
|
|
29
|
+
const _saveDataToDb = async (): Promise<void> => {
|
|
30
|
+
const appDb = BaseDb.getAppDb()
|
|
31
|
+
|
|
32
|
+
// Write fetched data from EAS to the database
|
|
33
|
+
await appDb.run(
|
|
34
|
+
sql.raw(
|
|
35
|
+
`INSERT INTO ${modelTableName} (version_local_id, seed_local_id, version_uid, created_at)
|
|
36
|
+
VALUES ('${versionLocalId}', '${seedLocalId}', '${versionUid}', ${new Date().getTime()});`,
|
|
37
|
+
),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const existingItemQuery = await appDb.run(
|
|
41
|
+
sql.raw(
|
|
42
|
+
`SELECT id, version_local_id, version_uid, seed_local_id, created_at
|
|
43
|
+
FROM ${modelTableName}
|
|
44
|
+
WHERE version_local_id = '${versionLocalId}';`,
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
let itemDbId
|
|
49
|
+
|
|
50
|
+
if (
|
|
51
|
+
existingItemQuery &&
|
|
52
|
+
existingItemQuery.rows &&
|
|
53
|
+
existingItemQuery.rows.length > 0
|
|
54
|
+
) {
|
|
55
|
+
itemDbId = existingItemQuery.rows[0][0]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (typeof itemDbId === 'undefined') {
|
|
59
|
+
console.error(
|
|
60
|
+
'[singleItemActors] [saveDataToDb] itemDbId not found in rows: ',
|
|
61
|
+
existingItemQuery.rows,
|
|
62
|
+
)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const [schemaUid, properties] of Object.entries(
|
|
67
|
+
propertiesBySchemaUid,
|
|
68
|
+
)) {
|
|
69
|
+
for (const property of properties) {
|
|
70
|
+
const json = JSON.parse(property.decodedDataJson)
|
|
71
|
+
const attestationValue = json[0].value
|
|
72
|
+
let propertyValue = attestationValue.value
|
|
73
|
+
|
|
74
|
+
if (typeof propertyValue === 'string') {
|
|
75
|
+
propertyValue = escapeSqliteString(propertyValue)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const camelCasePropertyName = relatedSnakeCaseToCamelCase(
|
|
79
|
+
attestationValue.name,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
let attestationCreatedAt = property.timeCreated * 1000
|
|
83
|
+
|
|
84
|
+
await appDb.run(
|
|
85
|
+
sql.raw(
|
|
86
|
+
`INSERT INTO metadata (uid,
|
|
87
|
+
local_id,
|
|
88
|
+
property_name,
|
|
89
|
+
property_value,
|
|
90
|
+
model_type,
|
|
91
|
+
schema_uid,
|
|
92
|
+
version_uid,
|
|
93
|
+
eas_data_type,
|
|
94
|
+
created_at,
|
|
95
|
+
attestation_created_at)
|
|
96
|
+
VALUES ('${property.id}', '${generateId()}', '${attestationValue.name}', '${propertyValue}',
|
|
97
|
+
'${modelTableName}', '${schemaUid}',
|
|
98
|
+
'${versionUid}',
|
|
99
|
+
'${attestationValue.type}',
|
|
100
|
+
${new Date().getTime()}, ${attestationCreatedAt}, ${itemDbId})
|
|
101
|
+
ON CONFLICT DO NOTHING;`,
|
|
102
|
+
),
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
// sendBack({
|
|
106
|
+
// type: 'updateValue',
|
|
107
|
+
// propertyName: camelCasePropertyName,
|
|
108
|
+
// propertyValue,
|
|
109
|
+
// source: 'eas',
|
|
110
|
+
// })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// const propertiesQuery = (await appDb.run(
|
|
114
|
+
// sql.raw(
|
|
115
|
+
// `SELECT property_name, property_value, schema_uid
|
|
116
|
+
// FROM metadata
|
|
117
|
+
// WHERE version_uid = '${versionUid}'
|
|
118
|
+
// ORDER BY created_at DESC;`,
|
|
119
|
+
// ),
|
|
120
|
+
// )) as { rows: Array<[string, string]> }
|
|
121
|
+
|
|
122
|
+
// propertiesQuery.rows?.forEach((row) => {
|
|
123
|
+
// const propertyName = relatedSnakeCaseToCamelCase(row[0] as string)
|
|
124
|
+
// let propertyValue = row[1] as string
|
|
125
|
+
// const schemaUid = row[2] as string
|
|
126
|
+
// if (isJSONString(propertyValue)) {
|
|
127
|
+
// propertyValue = JSON.parse(propertyValue)
|
|
128
|
+
// }
|
|
129
|
+
// sendBack({
|
|
130
|
+
// type: 'updateValue',
|
|
131
|
+
// propertyName,
|
|
132
|
+
// propertyValue,
|
|
133
|
+
// schemaUid,
|
|
134
|
+
// source: 'db',
|
|
135
|
+
// })
|
|
136
|
+
// })
|
|
137
|
+
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
_saveDataToDb().then(() => {
|
|
142
|
+
sendBack({ type: 'saveDataToDbSuccess' })
|
|
143
|
+
})
|
|
144
|
+
},
|
|
145
|
+
)
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput } from '@/types/machines'
|
|
3
|
+
import {
|
|
4
|
+
ItemPropertyValueType,
|
|
5
|
+
PropertyMachineContext,
|
|
6
|
+
SaveValueToDbEvent,
|
|
7
|
+
} from '@/types/property'
|
|
8
|
+
import { createSeed } from '@/db/write/createSeed'
|
|
9
|
+
import { getDataTypeFromString, getMimeType } from '@/helpers'
|
|
10
|
+
import { createVersion } from '@/db/write/createVersion'
|
|
11
|
+
import fs from '@zenfs/core'
|
|
12
|
+
import { createMetadata } from '@/db/write/createMetadata'
|
|
13
|
+
import { updateItemPropertyValue } from '@/db/write/updateItemPropertyValue'
|
|
14
|
+
import { getSchemaUidForSchemaDefinition } from '@/stores/eas'
|
|
15
|
+
import { getSchemaUidForModel } from '@/db/read/getSchemaUidForModel'
|
|
16
|
+
import { BaseFileManager } from '@/helpers/FileManager/BaseFileManager'
|
|
17
|
+
import { eventEmitter } from '@/eventBus'
|
|
18
|
+
import { ImageSize } from '@/helpers/constants'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const readFileAsArrayBuffer = async (file: File): Promise<ArrayBuffer> => {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
const reader = new FileReader()
|
|
25
|
+
reader.onload = async (e) => {
|
|
26
|
+
const arrayBuffer = e.target.result as ArrayBuffer
|
|
27
|
+
|
|
28
|
+
resolve(arrayBuffer)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
reader.readAsArrayBuffer(file)
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const fetchImage = async (url: string) => {
|
|
36
|
+
const response = await fetch(url)
|
|
37
|
+
const mimeType = response.headers.get('Content-Type')
|
|
38
|
+
const imageBuffer = await response.arrayBuffer()
|
|
39
|
+
const bytes = new Uint8Array(imageBuffer)
|
|
40
|
+
|
|
41
|
+
const binaryString = bytes.reduce(
|
|
42
|
+
(acc, byte) => acc + String.fromCharCode(byte),
|
|
43
|
+
'',
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
let base64 = btoa(binaryString)
|
|
47
|
+
|
|
48
|
+
if (mimeType) {
|
|
49
|
+
base64 = `data:${mimeType};base64,${base64}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return base64
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let imageSchemaUid: string | undefined
|
|
56
|
+
|
|
57
|
+
export const saveImageSrc = fromCallback<
|
|
58
|
+
EventObject,
|
|
59
|
+
FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>
|
|
60
|
+
>(({ sendBack, input: { context, event } }) => {
|
|
61
|
+
const {
|
|
62
|
+
localId,
|
|
63
|
+
propertyName: propertyNameRaw,
|
|
64
|
+
propertyValue: existingValue,
|
|
65
|
+
propertyRecordSchema,
|
|
66
|
+
modelName,
|
|
67
|
+
seedLocalId,
|
|
68
|
+
seedUid,
|
|
69
|
+
versionLocalId,
|
|
70
|
+
versionUid,
|
|
71
|
+
} = context
|
|
72
|
+
|
|
73
|
+
let { schemaUid } = context
|
|
74
|
+
|
|
75
|
+
let newValue: ItemPropertyValueType
|
|
76
|
+
|
|
77
|
+
if (event) {
|
|
78
|
+
newValue = event.newValue
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (existingValue === newValue) {
|
|
82
|
+
sendBack({ type: 'saveValueToDbSuccess' })
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const _saveImageSrc = async (): Promise<void> => {
|
|
87
|
+
let propertyName = propertyNameRaw
|
|
88
|
+
|
|
89
|
+
if (!propertyNameRaw.endsWith('Id')) {
|
|
90
|
+
propertyName = `${propertyName}Id`
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let newValueType
|
|
94
|
+
let fileData: string | ArrayBuffer | undefined
|
|
95
|
+
let mimeType
|
|
96
|
+
let fileName
|
|
97
|
+
|
|
98
|
+
if (!imageSchemaUid) {
|
|
99
|
+
imageSchemaUid = await getSchemaUidForModel('Image')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof newValue === 'string') {
|
|
103
|
+
newValueType = getDataTypeFromString(newValue)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (newValueType === 'imageBase64') {
|
|
107
|
+
mimeType = getMimeType(newValue as string)
|
|
108
|
+
const base64Data = (newValue as string).split(',')[1] // Strip the Base64 prefix
|
|
109
|
+
const binaryString = atob(base64Data)
|
|
110
|
+
|
|
111
|
+
const binaryLength = binaryString.length
|
|
112
|
+
const binaryArray = new Uint8Array(binaryLength)
|
|
113
|
+
for (let i = 0; i < binaryLength; i++) {
|
|
114
|
+
binaryArray[i] = binaryString.charCodeAt(i)
|
|
115
|
+
}
|
|
116
|
+
fileData = binaryArray.buffer
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (newValueType === 'url') {
|
|
120
|
+
fileData = await fetchImage(newValue as string)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (newValue instanceof File) {
|
|
124
|
+
fileName = newValue.name
|
|
125
|
+
mimeType = newValue.type
|
|
126
|
+
fileData = await readFileAsArrayBuffer(newValue)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!fileData) {
|
|
130
|
+
throw new Error('No file data found')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const newImageSeedLocalId = await createSeed({
|
|
134
|
+
type: 'image',
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
if (!fileName) {
|
|
138
|
+
fileName = newImageSeedLocalId
|
|
139
|
+
if (mimeType) {
|
|
140
|
+
fileName += `.${mimeType.split('/')[1]}`
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const filePath = `/files/images/${fileName}`
|
|
145
|
+
|
|
146
|
+
const imageVersionLocalId = await createVersion({
|
|
147
|
+
seedLocalId: newImageSeedLocalId,
|
|
148
|
+
seedType: 'image',
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
if (fileData instanceof ArrayBuffer) {
|
|
152
|
+
try {
|
|
153
|
+
await BaseFileManager.saveFile(filePath, new Uint8Array(fileData))
|
|
154
|
+
} catch (e) {
|
|
155
|
+
fs.writeFileSync(filePath, new Uint8Array(fileData))
|
|
156
|
+
eventEmitter.emit('file-saved', filePath)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (typeof fileData === 'string') {
|
|
161
|
+
try {
|
|
162
|
+
await BaseFileManager.saveFile(filePath, fileData)
|
|
163
|
+
} catch (e) {
|
|
164
|
+
fs.writeFileSync(filePath, fileData)
|
|
165
|
+
eventEmitter.emit('file-saved', filePath)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
await BaseFileManager.resizeImage({filePath, width: ImageSize.EXTRA_SMALL, height: ImageSize.EXTRA_SMALL})
|
|
171
|
+
await BaseFileManager.resizeImage({filePath, width: ImageSize.SMALL, height: ImageSize.SMALL})
|
|
172
|
+
await BaseFileManager.resizeImage({filePath, width: ImageSize.MEDIUM, height: ImageSize.MEDIUM})
|
|
173
|
+
await BaseFileManager.resizeImage({filePath, width: ImageSize.LARGE, height: ImageSize.LARGE})
|
|
174
|
+
await BaseFileManager.resizeImage({filePath, width: ImageSize.EXTRA_LARGE, height: ImageSize.EXTRA_LARGE})
|
|
175
|
+
|
|
176
|
+
const refResolvedDisplayValue = await BaseFileManager.getContentUrlFromPath(filePath)
|
|
177
|
+
|
|
178
|
+
let newLocalId
|
|
179
|
+
|
|
180
|
+
if (!localId) {
|
|
181
|
+
const result = await createMetadata(
|
|
182
|
+
{
|
|
183
|
+
propertyName,
|
|
184
|
+
propertyValue: newImageSeedLocalId,
|
|
185
|
+
seedLocalId,
|
|
186
|
+
seedUid,
|
|
187
|
+
versionLocalId,
|
|
188
|
+
versionUid,
|
|
189
|
+
modelName,
|
|
190
|
+
schemaUid,
|
|
191
|
+
refSeedType: 'image',
|
|
192
|
+
refModelUid: imageSchemaUid,
|
|
193
|
+
refSchemaUid: imageSchemaUid,
|
|
194
|
+
refResolvedDisplayValue,
|
|
195
|
+
refResolvedValue: fileName,
|
|
196
|
+
localStorageDir: '/images',
|
|
197
|
+
easDataType: 'bytes32',
|
|
198
|
+
},
|
|
199
|
+
propertyRecordSchema,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
newLocalId = result[0].localId
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (localId) {
|
|
206
|
+
await updateItemPropertyValue({
|
|
207
|
+
localId: localId,
|
|
208
|
+
propertyName: propertyNameRaw,
|
|
209
|
+
newValue: newImageSeedLocalId,
|
|
210
|
+
seedLocalId,
|
|
211
|
+
versionLocalId,
|
|
212
|
+
modelName,
|
|
213
|
+
schemaUid,
|
|
214
|
+
refSeedType: 'image',
|
|
215
|
+
refResolvedDisplayValue,
|
|
216
|
+
refResolvedValue: fileName,
|
|
217
|
+
refModelUid: imageSchemaUid,
|
|
218
|
+
refSchemaUid: imageSchemaUid,
|
|
219
|
+
localStorageDir: '/images',
|
|
220
|
+
easDataType: 'bytes32',
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
sendBack({
|
|
225
|
+
type: 'updateContext',
|
|
226
|
+
localId: newLocalId || localId,
|
|
227
|
+
propertyValue: newImageSeedLocalId,
|
|
228
|
+
refSeedType: 'image',
|
|
229
|
+
refSchemaUid: imageSchemaUid,
|
|
230
|
+
renderValue: refResolvedDisplayValue,
|
|
231
|
+
refResolvedDisplayValue,
|
|
232
|
+
refResolvedValue: fileName,
|
|
233
|
+
localStorageDir: '/images',
|
|
234
|
+
easDataType: 'bytes32',
|
|
235
|
+
schemaUid,
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
_saveImageSrc().then(() => {
|
|
240
|
+
sendBack({ type: 'saveImageSrcSuccess' })
|
|
241
|
+
})
|
|
242
|
+
})
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput } from '@/types/machines'
|
|
3
|
+
import {
|
|
4
|
+
ItemPropertyValueType,
|
|
5
|
+
PropertyMachineContext,
|
|
6
|
+
SaveValueToDbEvent,
|
|
7
|
+
} from '@/types/property'
|
|
8
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
9
|
+
import { getItemPropertyData } from '@/db/read/getItemProperty'
|
|
10
|
+
import { getItemData } from '@/db/read/getItemData'
|
|
11
|
+
import { and, eq } from 'drizzle-orm'
|
|
12
|
+
import { metadata } from '@/seedSchema'
|
|
13
|
+
import { createMetadata } from '@/db/write/createMetadata'
|
|
14
|
+
import fs from '@zenfs/core'
|
|
15
|
+
import { BaseFileManager } from '@/helpers/FileManager/BaseFileManager'
|
|
16
|
+
|
|
17
|
+
export const saveItemStorage = fromCallback<
|
|
18
|
+
EventObject,
|
|
19
|
+
FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>
|
|
20
|
+
>(({ sendBack, input: { context, event } }) => {
|
|
21
|
+
const {
|
|
22
|
+
localId,
|
|
23
|
+
seedLocalId,
|
|
24
|
+
seedUid,
|
|
25
|
+
propertyName,
|
|
26
|
+
propertyRecordSchema,
|
|
27
|
+
modelName,
|
|
28
|
+
propertyValue: existingValue,
|
|
29
|
+
} = context
|
|
30
|
+
|
|
31
|
+
if (!propertyRecordSchema) {
|
|
32
|
+
throw new Error('Missing propertyRecordSchema')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let newValue: ItemPropertyValueType
|
|
36
|
+
|
|
37
|
+
if (event) {
|
|
38
|
+
newValue = event.newValue
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (existingValue === newValue) {
|
|
42
|
+
sendBack({ type: 'saveValueToDbSuccess' })
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const _saveItemStorage = async (): Promise<boolean> => {
|
|
47
|
+
// Save value to file
|
|
48
|
+
const appDb = BaseDb.getAppDb()
|
|
49
|
+
let propertyData
|
|
50
|
+
|
|
51
|
+
if (localId) {
|
|
52
|
+
propertyData = await getItemPropertyData({
|
|
53
|
+
localId,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!localId && seedLocalId) {
|
|
58
|
+
const itemData = await getItemData({
|
|
59
|
+
seedLocalId,
|
|
60
|
+
})
|
|
61
|
+
if (itemData) {
|
|
62
|
+
const whereClauses = [
|
|
63
|
+
eq(metadata.propertyName, propertyName),
|
|
64
|
+
eq(metadata.seedLocalId, seedLocalId),
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
if (itemData.latestVersionLocalId) {
|
|
68
|
+
whereClauses.push(
|
|
69
|
+
eq(metadata.versionLocalId, itemData.latestVersionLocalId),
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const queryRows = await appDb
|
|
74
|
+
.select()
|
|
75
|
+
.from(metadata)
|
|
76
|
+
.where(and(...whereClauses))
|
|
77
|
+
|
|
78
|
+
if (queryRows && queryRows.length) {
|
|
79
|
+
propertyData = queryRows[0]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!propertyData && newValue) {
|
|
83
|
+
|
|
84
|
+
const filename = `${seedUid || seedLocalId}${propertyRecordSchema.filenameSuffix}`
|
|
85
|
+
const writeToPath = `/files/${propertyRecordSchema.localStorageDir}/${filename}`
|
|
86
|
+
await BaseFileManager.saveFile(writeToPath, newValue as string | Blob | ArrayBuffer)
|
|
87
|
+
|
|
88
|
+
const propertyDataRows = await createMetadata(
|
|
89
|
+
{
|
|
90
|
+
propertyName,
|
|
91
|
+
propertyValue: filename,
|
|
92
|
+
modelType: modelName.toLowerCase(),
|
|
93
|
+
seedLocalId,
|
|
94
|
+
seedUid,
|
|
95
|
+
versionLocalId: itemData.latestVersionLocalId,
|
|
96
|
+
versionUid: itemData.latestVersionUid,
|
|
97
|
+
localStorageDir: propertyRecordSchema.localStorageDir,
|
|
98
|
+
refValueType: 'file',
|
|
99
|
+
},
|
|
100
|
+
propertyRecordSchema,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
propertyData = propertyDataRows[0]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// propertyData = {
|
|
107
|
+
// propertyName,
|
|
108
|
+
// seedLocalId,
|
|
109
|
+
// seedUid,
|
|
110
|
+
// versionLocalId: itemData.latestVersionLocalId,
|
|
111
|
+
// versionUid: itemData.latestVersionUid,
|
|
112
|
+
// schemaUid: itemData.schemaUid,
|
|
113
|
+
// }
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const localStorageDir =
|
|
118
|
+
propertyRecordSchema.localStorageDir || propertyData.localStorageDir
|
|
119
|
+
const fileName =
|
|
120
|
+
propertyData.refResolvedValue ||
|
|
121
|
+
`${propertyData.seedUid || propertyData.seedLocalId}${propertyRecordSchema.filenameSuffix}`
|
|
122
|
+
|
|
123
|
+
if (!localStorageDir || !fileName) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Missing localStorageDir: ${localStorageDir} or fileName: ${fileName}`,
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const filePath = `/files/${localStorageDir}/${fileName}`
|
|
130
|
+
try {
|
|
131
|
+
await BaseFileManager.saveFile(filePath, newValue as string | Blob | ArrayBuffer)
|
|
132
|
+
} catch (error) {
|
|
133
|
+
fs.writeFileSync(filePath, newValue)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
await appDb
|
|
137
|
+
.update(metadata)
|
|
138
|
+
.set({
|
|
139
|
+
refResolvedValue: fileName,
|
|
140
|
+
})
|
|
141
|
+
.where(eq(metadata.localId, propertyData.localId))
|
|
142
|
+
|
|
143
|
+
sendBack({
|
|
144
|
+
type: 'updateContext',
|
|
145
|
+
renderValue: newValue,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
return true
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
_saveItemStorage().then((success) => {
|
|
152
|
+
if (success) {
|
|
153
|
+
sendBack({ type: 'saveItemStorageSuccess' })
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { metadata, MetadataType } from '@/seedSchema'
|
|
2
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
3
|
+
import { eq } from 'drizzle-orm'
|
|
4
|
+
|
|
5
|
+
export const saveMetadata = async (
|
|
6
|
+
metadataRecord: Partial<MetadataType>,
|
|
7
|
+
metadataValues: Partial<MetadataType>,
|
|
8
|
+
) => {
|
|
9
|
+
const appDb = BaseDb.getAppDb()
|
|
10
|
+
|
|
11
|
+
await appDb
|
|
12
|
+
.update(metadata)
|
|
13
|
+
.set({
|
|
14
|
+
...metadataValues,
|
|
15
|
+
updatedAt: Date.now(),
|
|
16
|
+
})
|
|
17
|
+
.where(eq(metadata.localId, metadataRecord.localId))
|
|
18
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput, GlobalMachineContext } from '@/types/machines'
|
|
3
|
+
import { saveAppState } from '@/db/write/saveAppState'
|
|
4
|
+
|
|
5
|
+
export const savePublishService = fromCallback<
|
|
6
|
+
EventObject,
|
|
7
|
+
FromCallbackInput<GlobalMachineContext>
|
|
8
|
+
>(({ sendBack, input: { event, context } }) => {
|
|
9
|
+
const { publishItemService } = context
|
|
10
|
+
|
|
11
|
+
if (!publishItemService) {
|
|
12
|
+
sendBack({ type: 'savePublishServiceError' })
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const _savePublishService = async (): Promise<boolean> => {
|
|
17
|
+
await saveAppState(
|
|
18
|
+
`snapshot__publishItemService`,
|
|
19
|
+
JSON.stringify(publishItemService.getPersistedSnapshot()),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_savePublishService().then((success) => {
|
|
26
|
+
if (success) {
|
|
27
|
+
sendBack({ type: 'savePublishServiceSuccess' })
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput } from '@/types/machines'
|
|
3
|
+
import {
|
|
4
|
+
ItemPropertyValueType,
|
|
5
|
+
PropertyMachineContext,
|
|
6
|
+
SaveValueToDbEvent,
|
|
7
|
+
} from '@/types/property'
|
|
8
|
+
import { getDataTypeFromString } from '@/helpers'
|
|
9
|
+
|
|
10
|
+
export const saveRelation = fromCallback<
|
|
11
|
+
EventObject,
|
|
12
|
+
FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>
|
|
13
|
+
>(({ sendBack, input: { context, event } }) => {
|
|
14
|
+
const {
|
|
15
|
+
localId,
|
|
16
|
+
propertyName: propertyNameRaw,
|
|
17
|
+
versionLocalId,
|
|
18
|
+
seedUid,
|
|
19
|
+
seedLocalId,
|
|
20
|
+
propertyValue: existingValue,
|
|
21
|
+
propertyRecordSchema,
|
|
22
|
+
} = context
|
|
23
|
+
|
|
24
|
+
if (!propertyRecordSchema) {
|
|
25
|
+
throw new Error('Missing propertyRecordSchema')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let newValue: ItemPropertyValueType
|
|
29
|
+
|
|
30
|
+
if (event) {
|
|
31
|
+
newValue = event.newValue
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const _saveRelation = async (): Promise<boolean> => {
|
|
35
|
+
let refResolvedDisplayValue
|
|
36
|
+
let refSeedType
|
|
37
|
+
let propertyName = propertyNameRaw
|
|
38
|
+
let versionLocalIdToSave = versionLocalId
|
|
39
|
+
|
|
40
|
+
const refResolvedValue = newValue
|
|
41
|
+
|
|
42
|
+
if (!propertyName.endsWith('Id')) {
|
|
43
|
+
propertyName = `${propertyName}Id`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let newValueType
|
|
47
|
+
|
|
48
|
+
if (typeof newValue === 'string') {
|
|
49
|
+
newValueType = getDataTypeFromString(newValue)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (newValue instanceof File) {
|
|
53
|
+
newValueType = 'file'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (propertyRecordSchema.dataType === 'ImageSrc') {
|
|
57
|
+
sendBack({
|
|
58
|
+
type: 'saveImageSrc',
|
|
59
|
+
newValue,
|
|
60
|
+
newValueType,
|
|
61
|
+
})
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true
|
|
66
|
+
|
|
67
|
+
// let fileType
|
|
68
|
+
//
|
|
69
|
+
// const dirs = await fs.promises.readdir('/files')
|
|
70
|
+
//
|
|
71
|
+
// for (const dir of dirs) {
|
|
72
|
+
// const files = await fs.promises.readdir(`/files/${dir}`)
|
|
73
|
+
// if (newValue && files.includes(newValue as string)) {
|
|
74
|
+
// fileType = dir
|
|
75
|
+
// break
|
|
76
|
+
// }
|
|
77
|
+
// }
|
|
78
|
+
//
|
|
79
|
+
// if (newValue && fileType === 'images') {
|
|
80
|
+
// const filePath = `/files/images/${newValue}`
|
|
81
|
+
// refResolvedDisplayValue = await getContentUrlFromPath(filePath)
|
|
82
|
+
// refSeedType = 'image'
|
|
83
|
+
// newValue = await createSeed({
|
|
84
|
+
// type: refSeedType,
|
|
85
|
+
// })
|
|
86
|
+
// await createVersion({
|
|
87
|
+
// seedLocalId,
|
|
88
|
+
// seedUid,
|
|
89
|
+
// seedType: refSeedType,
|
|
90
|
+
// })
|
|
91
|
+
// }
|
|
92
|
+
//
|
|
93
|
+
// await updateItemPropertyValue({
|
|
94
|
+
// propertyLocalId: localId,
|
|
95
|
+
// propertyName,
|
|
96
|
+
// newValue,
|
|
97
|
+
// seedLocalId,
|
|
98
|
+
// refSeedType,
|
|
99
|
+
// refResolvedValue,
|
|
100
|
+
// refResolvedDisplayValue,
|
|
101
|
+
// versionLocalId,
|
|
102
|
+
// modelName: itemModelName,
|
|
103
|
+
// schemaUid,
|
|
104
|
+
// })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_saveRelation().then((isDone) => {
|
|
108
|
+
if (isDone) {
|
|
109
|
+
sendBack({ type: 'saveRelationSuccess' })
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
})
|