Skip to content
Snippets Groups Projects
check_img.js 1.25 KiB
Newer Older
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()