mirror of
https://github.com/Doodle3D/Doodle3D-Core.git
synced 2024-12-22 19:13:49 +01:00
add db to doc function
This commit is contained in:
parent
81b144b86d
commit
04cb04b615
1583
package-lock.json
generated
1583
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -21,6 +21,7 @@
|
|||||||
"bowser": "^1.8.1",
|
"bowser": "^1.8.1",
|
||||||
"imports-loader": "^0.7.1",
|
"imports-loader": "^0.7.1",
|
||||||
"memoizee": "^0.3.9",
|
"memoizee": "^0.3.9",
|
||||||
|
"pouchdb": "^6.3.4",
|
||||||
"proptypes": "^1.1.0",
|
"proptypes": "^1.1.0",
|
||||||
"raw-loader": "^0.5.1",
|
"raw-loader": "^0.5.1",
|
||||||
"react": "^16.0.0",
|
"react": "^16.0.0",
|
||||||
|
63
src/shape/dbToDoc.js
Normal file
63
src/shape/dbToDoc.js
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { blobToJSON } from '../utils/binaryUtils.js';
|
||||||
|
import JSONToSketchData from './JSONToSketchData.js';
|
||||||
|
import semver from 'semver';
|
||||||
|
|
||||||
|
export async function docToFile(db, doc, { image = false, sketch = false } = {}) {
|
||||||
|
// NOTE doc.appVersion can be used to check in which version of the app the file was saved in
|
||||||
|
const response = {
|
||||||
|
name: doc.name,
|
||||||
|
id: doc._id,
|
||||||
|
createdOn: doc.createdOn,
|
||||||
|
updatedOn: doc.updatedOn
|
||||||
|
};
|
||||||
|
|
||||||
|
const { appVersion } = doc;
|
||||||
|
|
||||||
|
const keys = [];
|
||||||
|
if (sketch && semver.gt(appVersion, '0.0.4')) keys.push('sketch');
|
||||||
|
|
||||||
|
if (keys.length > 0) await addAttachments(db, [doc], keys);
|
||||||
|
|
||||||
|
if (sketch) {
|
||||||
|
if (semver.gt(appVersion, '0.0.4')) {
|
||||||
|
if (!(doc._attachments && doc._attachments.sketch && doc._attachments.sketch.data)) {
|
||||||
|
throw new Error(`'${doc.name}' doesn't include sketch attachment`);
|
||||||
|
}
|
||||||
|
const data = await blobToJSON(doc._attachments.sketch.data);
|
||||||
|
response.data = await JSONToSketchData(data);
|
||||||
|
} else {
|
||||||
|
const data = { data: doc.data, appVersion };
|
||||||
|
response.data = await JSONToSketchData(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (image) {
|
||||||
|
response.img = `${db.name}/${doc._id}/img`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addAttachments(db, docs, keys) {
|
||||||
|
if (keys) {
|
||||||
|
keys = keys.reduce((obj, value) => {
|
||||||
|
obj[value] = null;
|
||||||
|
return obj;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
|
for (const { _attachments: attachments, _id: id } of docs) {
|
||||||
|
for (const attachmentId in keys || attachments) {
|
||||||
|
const attachment = attachments[attachmentId];
|
||||||
|
|
||||||
|
const promise = db.getAttachment(id, attachmentId).then(blob => {
|
||||||
|
attachment.data = blob;
|
||||||
|
});
|
||||||
|
|
||||||
|
promises.push(promise);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
|
import dbToDoc from './dbToDoc.js';
|
||||||
import shapeToPoints from './shapeToPoints.js';
|
import shapeToPoints from './shapeToPoints.js';
|
||||||
import JSONToSketchData from './JSONToSketchData.js';
|
import JSONToSketchData from './JSONToSketchData.js';
|
||||||
import ShapeDataUtils from './shapeDataUtils.js';
|
import ShapeDataUtils from './shapeDataUtils.js';
|
||||||
|
|
||||||
export { shapeToPoints, JSONToSketchData, ShapeDataUtils };
|
export { dbToDoc, shapeToPoints, JSONToSketchData, ShapeDataUtils };
|
||||||
|
Loading…
Reference in New Issue
Block a user