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:
		| @@ -79,6 +79,9 @@ tfrpc.register(async function store_blob(blob) { | ||||
| tfrpc.register(async function get_blob(id) { | ||||
| 	return utf8Decode(await ssb.blobGet(id)); | ||||
| }); | ||||
| tfrpc.register(async function store_message(message) { | ||||
| 	return await ssb.storeMessage(message); | ||||
| }); | ||||
| tfrpc.register(function apps() { | ||||
| 	return core.apps(); | ||||
| }); | ||||
|   | ||||
							
								
								
									
										3
									
								
								apps/ssb/filesaver.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								apps/ssb/filesaver.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| (function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(a,b,c){var d=new XMLHttpRequest;d.open("GET",a),d.responseType="blob",d.onload=function(){g(d.response,b,c)},d.onerror=function(){console.error("could not download file")},d.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),g=f.saveAs||("object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype&&!a?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(b,d,e,g){if(g=g||open("","_blank"),g&&(g.document.title=g.document.body.innerText="downloading..."),"string"==typeof b)return c(b,d,e);var h="application/octet-stream"===b.type,i=/constructor/i.test(f.HTMLElement)||f.safari,j=/CriOS\/[\d]+/.test(navigator.userAgent);if((j||h&&i||a)&&"undefined"!=typeof FileReader){var k=new FileReader;k.onloadend=function(){var a=k.result;a=j?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),g?g.location.href=a:location=a,g=null},k.readAsDataURL(b)}else{var l=f.URL||f.webkitURL,m=l.createObjectURL(b);g?g.location=m:location.href=m,g=null,setTimeout(function(){l.revokeObjectURL(m)},4E4)}});f.saveAs=g.saveAs=g,"undefined"!=typeof module&&(module.exports=g)}); | ||||
|  | ||||
| //# sourceMappingURL=FileSaver.min.js.map | ||||
| @@ -13,6 +13,8 @@ | ||||
| 	<body> | ||||
| 		<tf-app/> | ||||
| 		<script>window.litDisableBundleWarning = true;</script> | ||||
| 		<script src="filesaver.min.js"></script> | ||||
| 		<script src="jszip.min.js"></script> | ||||
| 		<script src="commonmark.min.js"></script> | ||||
| 		<script src="commonmark-linkify.js" type="module"></script> | ||||
| 		<script src="commonmark-hashtag.js" type="module"></script> | ||||
|   | ||||
							
								
								
									
										13
									
								
								apps/ssb/jszip.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								apps/ssb/jszip.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -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>`; | ||||
| 	} | ||||
|   | ||||
| @@ -12,6 +12,7 @@ class TfTabNewsElement extends LitElement { | ||||
| 			following: {type: Array}, | ||||
| 			drafts: {type: Object}, | ||||
| 			expanded: {type: Object}, | ||||
| 			import_progress: {type: Object}, | ||||
| 		}; | ||||
| 	} | ||||
|  | ||||
| @@ -28,6 +29,7 @@ class TfTabNewsElement extends LitElement { | ||||
| 		this.cache = {}; | ||||
| 		this.drafts = {}; | ||||
| 		this.expanded = {}; | ||||
| 		this.import_progress = null; | ||||
| 		tfrpc.rpc.localStorageGet('drafts').then(function(d) { | ||||
| 			self.drafts = JSON.parse(d || '{}'); | ||||
| 		}); | ||||
| @@ -102,12 +104,50 @@ class TfTabNewsElement extends LitElement { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	import_data() { | ||||
| 		let self = this; | ||||
| 		let input = document.createElement('input'); | ||||
| 		input.type = 'file'; | ||||
| 		input.onchange = async function(event) { | ||||
| 				self.import_progress = {name: 'loading'}; | ||||
| 			let zip = new JSZip(); | ||||
| 			let file = await zip.loadAsync(event.target.files[0]); | ||||
| 			let progress = 0; | ||||
| 			let messages = JSON.parse(await file.file('messages.txt').async('string')); | ||||
| 			for (let message of messages) { | ||||
| 				self.import_progress = {name: 'messages', value: progress++, max: messages.length}; | ||||
| 				await tfrpc.rpc.store_message(message); | ||||
| 			} | ||||
| 			let blobs = []; | ||||
| 			file.forEach(function(path, entry) { | ||||
| 				if (path != 'messages.txt' && !entry.dir) { | ||||
| 					blobs.push(entry); | ||||
| 				} | ||||
| 			}); | ||||
| 			progress = 0; | ||||
| 			for (let blob of blobs) { | ||||
| 				self.import_progress = {name: 'blobs', value: progress++, max: blobs.length}; | ||||
| 				console.log(await tfrpc.rpc.store_blob(await blob.async('arraybuffer'))); | ||||
| 			} | ||||
| 			self.import_progress = undefined; | ||||
| 		}; | ||||
| 		input.click(); | ||||
| 	} | ||||
|  | ||||
| 	render() { | ||||
| 		let profile = this.hash.startsWith('#@') ? | ||||
| 			html`<tf-profile id=${this.hash.substring(1)} whoami=${this.whoami} .users=${this.users}></tf-profile>` : undefined; | ||||
| 		let import_state = html`<input type="button" value="Import" @click=${this.import_data}></input>`; | ||||
| 		if (this.import_progress) { | ||||
| 			if (this.import_progress.max) { | ||||
| 				import_state = html`${this.import_progress.name} <progress value=${this.import_progress.value} max=${this.import_progress.max}></progress>`; | ||||
| 			} else { | ||||
| 				import_state = html`${this.import_progress.name} <progress></progress>`; | ||||
| 			} | ||||
| 		} | ||||
| 		return html` | ||||
| 			<div><input type="button" value=${this.new_messages_text()} @click=${this.show_more}></input></div> | ||||
| 			<a target="_top" href="#" ?hidden=${this.hash.length <= 1}>🏠Home</a> | ||||
| 			<a target="_top" href="#" ?hidden=${this.hash.length <= 1}>🏠Home</a> ${import_state} | ||||
| 			<div>Welcome, <tf-user id=${this.whoami} .users=${this.users}></tf-user>!</div> | ||||
| 			<div><tf-compose whoami=${this.whoami} .users=${this.users} .drafts=${this.drafts} @tf-draft=${this.draft}></tf-compose></div> | ||||
| 			${profile} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user