Screenshot handling simplification
- Log in preemptively; non-approved screenshots may not be visible to some users. - Run all access sequentially; unsure of CDB's concurrency handling for a given single user (e.g. are CSRF tokens invalidated).
This commit is contained in:
parent
0b72f05d16
commit
f04585eb1b
@ -30,12 +30,22 @@ async function cdbscreens() {
|
||||
|
||||
const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/';
|
||||
|
||||
await cdblib.login();
|
||||
console.log('checking existing screenshots...');
|
||||
const $ = await cdblib.fetch(uri);
|
||||
const exist = [];
|
||||
$('.screenshot_list img')
|
||||
.each((i, e) => exist.push($(e)
|
||||
.attr('alt')));
|
||||
$('.screenshot_list a')
|
||||
.each((i, e) => {
|
||||
const href = $(e)
|
||||
.attr('href');
|
||||
if(/screenshots\/new/.test(href))
|
||||
return;
|
||||
exist.push({
|
||||
id: $('img', e)
|
||||
.attr('alt'),
|
||||
key: href.match(/screenshots\/(\d+)\/edit/)[1]
|
||||
});
|
||||
});
|
||||
|
||||
let dirty;
|
||||
|
||||
@ -43,21 +53,20 @@ async function cdbscreens() {
|
||||
let spos = 0;
|
||||
const deletes = [];
|
||||
while(epos < exist.length && spos < screens.length) {
|
||||
if(exist[epos] === screens[spos].id)
|
||||
if(exist[epos].id === screens[spos].id)
|
||||
spos++;
|
||||
else
|
||||
deletes.push(epos);
|
||||
deletes.push(exist[epos].key);
|
||||
epos++;
|
||||
}
|
||||
while(epos < exist.length) {
|
||||
deletes.push(epos);
|
||||
deletes.push(exist[epos].key);
|
||||
epos++;
|
||||
}
|
||||
while(spos < screens.length) {
|
||||
dirty = true;
|
||||
const scr = screens[spos];
|
||||
console.log(`uploading ${scr.name || scr.id}...`);
|
||||
await cdblib.login();
|
||||
const newuri = uri + 'screenshots/new/';
|
||||
const $$ = await cdblib.fetch(newuri);
|
||||
const fields = cdblib.getfields($$);
|
||||
@ -74,36 +83,21 @@ async function cdbscreens() {
|
||||
await cdblib.fetch(newuri, 'post', fields, { multipart: true });
|
||||
spos++
|
||||
}
|
||||
|
||||
if(deletes.length) {
|
||||
await cdblib.login();
|
||||
const $$ = await cdblib.fetch(uri);
|
||||
const imgids = [];
|
||||
$$('.screenshot_list a')
|
||||
.each((i, e) => {
|
||||
const href = $$(e)
|
||||
.attr('href');
|
||||
if(/screenshots\/new/.test(href))
|
||||
return;
|
||||
imgids.push(href.match(/screenshots\/(\d+)\/edit/)[1])
|
||||
});
|
||||
await Promise.all(deletes
|
||||
.map(i => imgids[i])
|
||||
.map(async id => {
|
||||
dirty = true;
|
||||
console.log(`deleting screenshot ${id}...`)
|
||||
const edituri = uri + 'screenshots/' + id + '/edit/';
|
||||
const $$$ = await cdblib.fetch(edituri);
|
||||
const fields = cdblib.getfields($$$);
|
||||
fields.delete = 'y';
|
||||
delete fields.approved;
|
||||
console.log(fields);
|
||||
if(config.dryrun) {
|
||||
console.log('dry run; not deleting');
|
||||
console.log(util.inspect(fields));
|
||||
} else
|
||||
await cdblib.fetch(edituri, 'post', fields, { multipart: true });
|
||||
}));
|
||||
for(let i = 0; i < deletes.length; i++) {
|
||||
const key = deletes[i];
|
||||
dirty = true;
|
||||
console.log(`deleting screenshot ${key}...`)
|
||||
const edituri = uri + 'screenshots/' + key + '/edit/';
|
||||
const $$ = await cdblib.fetch(edituri);
|
||||
const fields = cdblib.getfields($$);
|
||||
fields.delete = 'y';
|
||||
delete fields.approved;
|
||||
console.log(fields);
|
||||
if(config.dryrun) {
|
||||
console.log('dry run; not deleting');
|
||||
console.log(util.inspect(fields));
|
||||
} else
|
||||
await cdblib.fetch(edituri, 'post', fields, { multipart: true });
|
||||
}
|
||||
|
||||
console.log('screenshots synced');
|
||||
|
Loading…
x
Reference in New Issue
Block a user