🐛 修复 #264

 支持 db.postAttachment 和 db.getAttachment
♻️ 重构版本更新机制
This commit is contained in:
muwoo
2023-10-18 11:02:14 +08:00
parent a7a85a7c62
commit bde5377282
12 changed files with 195 additions and 120 deletions

View File

@@ -204,4 +204,38 @@ export default class DB {
this.pouchDB = syncDb.pouchDB;
await webdavClient.createReadStream(this.pouchDB);
}
public async postAttachment(
name: string,
docId: string,
attachment: Buffer | Uint8Array,
type: string
) {
const buffer = Buffer.from(attachment);
if (buffer.byteLength > this.docAttachmentMaxByteLength)
return this.errorInfo(
'exception',
'attachment data up to ' +
this.docAttachmentMaxByteLength / 1024 / 1024 +
'M'
);
try {
const result = await this.pouchDB.put({
_id: this.getDocId(name, docId),
_attachments: { 0: { data: buffer, content_type: type } },
});
result.id = this.replaceDocId(name, result.id);
return result;
} catch (e) {
return this.errorInfo(e.name, e.message);
}
}
async getAttachment(name: string, docId: string, len = '0') {
try {
return await this.pouchDB.getAttachment(this.getDocId(name, docId), len);
} catch (e) {
return null;
}
}
}

View File

@@ -11,8 +11,8 @@ type WebDavOptions = {
};
type DBInstance = {
loadIt: (stream: unknown) => void;
dump: (stream: unknown) => void;
loadIt: (stream: unknown, options?: any) => void;
dump: (stream: unknown, options?: any) => void;
};
export default class WebDav {
@@ -54,7 +54,12 @@ export default class WebDav {
}).show();
}
const ws = new MemoryStream();
dbInstance.dump(ws);
dbInstance.dump(ws, {
filter: (doc) => {
// attachment 文档导出有问题,
return !doc._attachments;
},
});
ws.pipe(
this.client.createWriteStream(this.cloudPath, {}, () => {
new Notification({

View File

@@ -157,13 +157,17 @@ class AdapterHandler {
*/
private async execCommand(cmd: string, modules: string[]): Promise<string> {
return new Promise((resolve: any, reject: any) => {
const args: string[] = [cmd]
.concat(
cmd !== 'uninstall' ? modules.map((m) => `${m}@latest`) : modules
)
.concat('--color=always')
.concat('--save')
.concat(`--registry=${this.registry}`);
let args: string[] = [cmd].concat(
cmd !== 'uninstall' && cmd !== 'link'
? modules.map((m) => `${m}@latest`)
: modules
);
if (cmd !== 'link') {
args = args
.concat('--color=always')
.concat('--save')
.concat(`--registry=${this.registry}`);
}
const npm = spawn('npm', args, {
cwd: this.baseDir,