|
|
@ -69,38 +69,40 @@ const migrate = (authFileName = 'auth.json'): Promise<Users> => new Promise(asyn
|
|
|
|
.catch(reject)
|
|
|
|
.catch(reject)
|
|
|
|
|
|
|
|
|
|
|
|
// Migrate the datafile (token => uploader)
|
|
|
|
// Migrate the datafile (token => uploader)
|
|
|
|
.then(() => data().get())
|
|
|
|
.then(() => (!data())
|
|
|
|
.then((fileData: [string, FileData][]) =>
|
|
|
|
? (log.warn('data.json not found. This may be a new install?'), Promise.resolve())
|
|
|
|
|
|
|
|
: data().get().then((fileData: [string, FileData][]) =>
|
|
|
|
|
|
|
|
|
|
|
|
// ! A note about this block.
|
|
|
|
// ! A note about this block.
|
|
|
|
// I know it's gross. But using Promise.all crashes low-spec servers, so I had to do it this way. Sorry.
|
|
|
|
// I know it's gross. But using Promise.all crashes low-spec servers, so I had to do it this way. Sorry.
|
|
|
|
// Thanks to CoPilot for writing `runQueue` :D
|
|
|
|
// Thanks to CoPilot for writing `runQueue` :D
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for all the deletions and puts to finish
|
|
|
|
// Wait for all the deletions and puts to finish
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
|
|
|
|
// Create a queue of functions to run
|
|
|
|
// Create a queue of functions to run
|
|
|
|
const queue = fileData.map(([key, file]) => async () => {
|
|
|
|
const queue = fileData.map(([key, file]) => async () => {
|
|
|
|
|
|
|
|
|
|
|
|
// We need to use `newUsers` because `users` hasn't been re-assigned yet
|
|
|
|
// We need to use `newUsers` because `users` hasn't been re-assigned yet
|
|
|
|
const user = newUsers.users.find((user) => user.token === file.token!)?.unid ?? ''; // ? This is probably fine
|
|
|
|
const user = newUsers.users.find((user) => user.token === file.token!)?.unid ?? ''; // ? This is probably fine
|
|
|
|
|
|
|
|
|
|
|
|
// Because of the stupid way I wrote papito, we need to DEL before we can PUT
|
|
|
|
// Because of the stupid way I wrote papito, we need to DEL before we can PUT
|
|
|
|
await data().del(key);
|
|
|
|
await data().del(key);
|
|
|
|
|
|
|
|
|
|
|
|
// PUT the new data
|
|
|
|
// PUT the new data
|
|
|
|
return data().put(key, { ...file, uploader: user });
|
|
|
|
return data().put(key, { ...file, uploader: user });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Recursively run the queue, hopefully sequentially without running out of memory
|
|
|
|
// Recursively run the queue, hopefully sequentially without running out of memory
|
|
|
|
const runQueue = (index: number) => {
|
|
|
|
const runQueue = (index: number) => {
|
|
|
|
if (index >= queue.length) return resolve(void 0);
|
|
|
|
if (index >= queue.length) return resolve(void 0);
|
|
|
|
queue[index]().then(() => runQueue(index + 1)).catch(reject);
|
|
|
|
queue[index]().then(() => runQueue(index + 1)).catch(reject);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
runQueue(0);
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runQueue(0);
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
.catch((err: any) => log.warn(err.message))
|
|
|
|
|
|
|
|
)
|
|
|
|
// We did it hoofuckingray
|
|
|
|
// We did it hoofuckingray
|
|
|
|
.then(() => log.success('Migrated all auth & file data to new auth system'))
|
|
|
|
.then(() => log.success('Migrated all auth & file data to new auth system'))
|
|
|
|
.then(() => resolve(newUsers))
|
|
|
|
.then(() => resolve(newUsers))
|
|
|
|