diff --git a/scripts/check_img.js b/scripts/check_img.js new file mode 100644 index 0000000000000000000000000000000000000000..b0f74206de785d87c67d8681765db7bfc8e234b6 --- /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