From 08492502aaa78f8a5fae3794b9543877f1ec2f28 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Wed, 8 Dec 2021 15:58:28 +0000
Subject: [PATCH] Script for checking upload status

---
 scripts/check_img.js | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 scripts/check_img.js

diff --git a/scripts/check_img.js b/scripts/check_img.js
new file mode 100644
index 0000000..b0f7420
--- /dev/null
+++ b/scripts/check_img.js
@@ -0,0 +1,44 @@
+async function main() {
+	const { URL } = require('url')
+	const { Upload } = require('database/models')
+	const { fs } = require('services')
+	let uploads = []
+	const total = await Upload.count({ where: { status: 'pending' } })
+	let count = 0
+
+	uploads = await Upload.findAll({ limit: 25, order: ['created_at'], where: { status: 'pending' } })
+	while (uploads.length > 0) {
+		console.log('Processing entries %d to %d, of total %d', count, count + uploads.length, total)
+		count += uploads.length
+
+		for (const upload of uploads) {
+			await new Promise(r => setTimeout(r, 50))
+			// const path = `${ upload.user_id }/${ upload.id }.jpg`
+			const url = new URL(upload.upload_url)
+			url.search = ''
+
+			const [_, bucket, ...pathParts] = url.pathname.split('/')
+			const path = pathParts.join('/')
+
+			try {
+				await fs.makePublic(path)
+			} catch(e) {
+				console.error('Couldnt make ', path, ' public')
+				upload.status = 'failed'
+				upload.status_reason = 'bogus upload url'
+				await upload.save()
+				continue
+			}
+
+			console.log(url.toString())
+			upload.status = 'success'
+			await upload.save()
+		}
+		await new Promise(r => setTimeout(r, 500))
+		uploads = await Upload.findAll({ limit: 25, order: ['created_at'], where: { status: 'pending' } })
+	}
+
+	process.exit(0)
+}
+
+main()
\ No newline at end of file
-- 
GitLab