forked from cory/tildefriends
Work in progress zip import/export.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4287 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -11,6 +11,7 @@ class TfProfileElement extends LitElement {
|
||||
id: {type: String},
|
||||
users: {type: Object},
|
||||
size: {type: Number},
|
||||
export_progress: {type: Object},
|
||||
};
|
||||
}
|
||||
|
||||
@ -24,6 +25,7 @@ class TfProfileElement extends LitElement {
|
||||
this.id = null;
|
||||
this.users = {};
|
||||
this.size = 0;
|
||||
this.export_progress = null;
|
||||
}
|
||||
|
||||
modify(change) {
|
||||
@ -103,6 +105,66 @@ class TfProfileElement extends LitElement {
|
||||
input.click();
|
||||
}
|
||||
|
||||
format_message(message) {
|
||||
let out = {
|
||||
previous: message.previous ?? null,
|
||||
};
|
||||
if (message.sequence_before_author) {
|
||||
out.sequence = message.sequence;
|
||||
out.author = message.author;
|
||||
} else {
|
||||
out.author = message.author;
|
||||
out.sequence = message.sequence;
|
||||
}
|
||||
out.timestamp = message.timestamp;
|
||||
out.hash = message.hash;
|
||||
out.content = message.content;
|
||||
out.signature = message.signature;
|
||||
return out;
|
||||
}
|
||||
|
||||
async export() {
|
||||
let all_messages = [];
|
||||
let sequence = -1;
|
||||
let messages_max = (await tfrpc.rpc.query('SELECT MAX(sequence) FROM messages WHERE author = ?', [this.id]))[0].sequence;
|
||||
while (true) {
|
||||
let messages = await tfrpc.rpc.query(
|
||||
'SELECT * FROM messages WHERE author = ? AND SEQUENCE > ? ORDER BY sequence LIMIT 100',
|
||||
[this.id, sequence]
|
||||
);
|
||||
if (messages?.length) {
|
||||
all_messages = [].concat(all_messages, messages.map(x => this.format_message(x)));
|
||||
sequence = messages[messages.length - 1].sequence;
|
||||
this.export_progress = {name: 'messages', value: all_messages.length, max: messages_max};
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let zip = new JSZip();
|
||||
zip.file('messages.txt', JSON.stringify(all_messages, null, 2));
|
||||
|
||||
let blobs = await tfrpc.rpc.query(
|
||||
`SELECT blobs.id
|
||||
FROM messages
|
||||
JOIN messages_refs ON messages.id = messages_refs.message
|
||||
JOIN blobs ON messages_refs.ref = blobs.id
|
||||
WHERE messages.author = ?`,
|
||||
[this.id]);
|
||||
let blobs_done = 0;
|
||||
for (let row of blobs) {
|
||||
this.export_progress = {name: 'blobs', value: blobs_done, max: blobs.length};
|
||||
let blob = await tfrpc.rpc.get_blob(row.id);
|
||||
zip.folder('blobs').file(row.id.replaceAll('/', '_').replaceAll('+', '-'), blob);
|
||||
blobs_done++;
|
||||
}
|
||||
|
||||
this.export_progress = {name: 'saving'};
|
||||
let blob = await zip.generateAsync({type: 'blob'});
|
||||
saveAs(blob, `${this.id.replaceAll('/', '_').replaceAll('+', '-')}.zip`);
|
||||
this.export_progress = null;
|
||||
}
|
||||
|
||||
render() {
|
||||
let self = this;
|
||||
let profile = this.users[this.id] || {};
|
||||
@ -150,6 +212,14 @@ class TfProfileElement extends LitElement {
|
||||
</div>
|
||||
<input type="button" value="Attach Image" @click=${this.attach_image}></input>
|
||||
</div>` : null;
|
||||
let export_state = html`<input type="button" value="Export" @click=${this.export}></input>`;
|
||||
if (this.export_progress) {
|
||||
if (this.export_progress.max) {
|
||||
export_state = html`<span>${this.export_progress.name}</span><progress value=${this.export_progress.value} max=${this.export_progress.max}></progress>`;
|
||||
} else {
|
||||
export_state = html`<span>${this.export_progress.name}</span>`;
|
||||
}
|
||||
}
|
||||
let image = typeof(profile.image) == 'string' ? profile.image : profile.image?.link;
|
||||
image = this.editing?.image ?? image;
|
||||
let description = this.editing?.description ?? profile.description;
|
||||
@ -172,6 +242,7 @@ class TfProfileElement extends LitElement {
|
||||
${edit}
|
||||
${follow}
|
||||
${block}
|
||||
${export_state}
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
Reference in New Issue
Block a user