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 + '/';
|
const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/';
|
||||||
|
|
||||||
|
await cdblib.login();
|
||||||
console.log('checking existing screenshots...');
|
console.log('checking existing screenshots...');
|
||||||
const $ = await cdblib.fetch(uri);
|
const $ = await cdblib.fetch(uri);
|
||||||
const exist = [];
|
const exist = [];
|
||||||
$('.screenshot_list img')
|
$('.screenshot_list a')
|
||||||
.each((i, e) => exist.push($(e)
|
.each((i, e) => {
|
||||||
.attr('alt')));
|
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;
|
let dirty;
|
||||||
|
|
||||||
@ -43,21 +53,20 @@ async function cdbscreens() {
|
|||||||
let spos = 0;
|
let spos = 0;
|
||||||
const deletes = [];
|
const deletes = [];
|
||||||
while(epos < exist.length && spos < screens.length) {
|
while(epos < exist.length && spos < screens.length) {
|
||||||
if(exist[epos] === screens[spos].id)
|
if(exist[epos].id === screens[spos].id)
|
||||||
spos++;
|
spos++;
|
||||||
else
|
else
|
||||||
deletes.push(epos);
|
deletes.push(exist[epos].key);
|
||||||
epos++;
|
epos++;
|
||||||
}
|
}
|
||||||
while(epos < exist.length) {
|
while(epos < exist.length) {
|
||||||
deletes.push(epos);
|
deletes.push(exist[epos].key);
|
||||||
epos++;
|
epos++;
|
||||||
}
|
}
|
||||||
while(spos < screens.length) {
|
while(spos < screens.length) {
|
||||||
dirty = true;
|
dirty = true;
|
||||||
const scr = screens[spos];
|
const scr = screens[spos];
|
||||||
console.log(`uploading ${scr.name || scr.id}...`);
|
console.log(`uploading ${scr.name || scr.id}...`);
|
||||||
await cdblib.login();
|
|
||||||
const newuri = uri + 'screenshots/new/';
|
const newuri = uri + 'screenshots/new/';
|
||||||
const $$ = await cdblib.fetch(newuri);
|
const $$ = await cdblib.fetch(newuri);
|
||||||
const fields = cdblib.getfields($$);
|
const fields = cdblib.getfields($$);
|
||||||
@ -74,36 +83,21 @@ async function cdbscreens() {
|
|||||||
await cdblib.fetch(newuri, 'post', fields, { multipart: true });
|
await cdblib.fetch(newuri, 'post', fields, { multipart: true });
|
||||||
spos++
|
spos++
|
||||||
}
|
}
|
||||||
|
for(let i = 0; i < deletes.length; i++) {
|
||||||
if(deletes.length) {
|
const key = deletes[i];
|
||||||
await cdblib.login();
|
dirty = true;
|
||||||
const $$ = await cdblib.fetch(uri);
|
console.log(`deleting screenshot ${key}...`)
|
||||||
const imgids = [];
|
const edituri = uri + 'screenshots/' + key + '/edit/';
|
||||||
$$('.screenshot_list a')
|
const $$ = await cdblib.fetch(edituri);
|
||||||
.each((i, e) => {
|
const fields = cdblib.getfields($$);
|
||||||
const href = $$(e)
|
fields.delete = 'y';
|
||||||
.attr('href');
|
delete fields.approved;
|
||||||
if(/screenshots\/new/.test(href))
|
console.log(fields);
|
||||||
return;
|
if(config.dryrun) {
|
||||||
imgids.push(href.match(/screenshots\/(\d+)\/edit/)[1])
|
console.log('dry run; not deleting');
|
||||||
});
|
console.log(util.inspect(fields));
|
||||||
await Promise.all(deletes
|
} else
|
||||||
.map(i => imgids[i])
|
await cdblib.fetch(edituri, 'post', fields, { multipart: true });
|
||||||
.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 });
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('screenshots synced');
|
console.log('screenshots synced');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user