aboutsummaryrefslogtreecommitdiff
path: root/src/server/remapUrl.ts
blob: ca7ca241f7a66f301949e9bb42886c82f19a7a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { URL } from 'url';
import { Database } from './database';
import { resolvedPorts } from './SocketData';

// npx ts-node src/server/remapUrl.ts

const suffixMap: { [type: string]: true } = {
    video: true,
    pdf: true,
    audio: true,
    web: true,
    image: true,
    map: true,
};

async function update() {
    await new Promise(res => {
        setTimeout(res, 10);
    });
    console.log('update');
    const cursor = await Database.Instance.query({});
    console.log('Cleared');
    const updates: [string, any][] = [];
    function updateDoc(doc: any) {
        if (doc.__type !== 'Doc') {
            return;
        }
        const { fields } = doc;
        if (!fields) {
            return;
        }
        const updated: any = {};
        let dynfield = false;
        Array.from(Object.keys(fields)).forEach(key => {
            const value = fields[key];
            if (value && value.__type && suffixMap[value.__type]) {
                const url = new URL(value.url);
                if (url.href.includes('localhost') && url.href.includes('Bill')) {
                    dynfield = true;

                    updated.$set = { ['fields.' + key + '.url']: `${url.protocol}//dash-web.eastus2.cloudapp.azure.com:${resolvedPorts.server}${url.pathname}` };
                }
            }
        });
        if (dynfield) {
            updates.push([doc._id, updated]);
        }
    }
    await cursor.forEach(updateDoc);
    await Promise.all(
        updates.map(doc => {
            console.log(doc[0], doc[1]);
            return new Promise<void>(res => {
                Database.Instance.update(
                    doc[0],
                    doc[1],
                    () => {
                        console.log('wrote ' + JSON.stringify(doc[1]));
                        res();
                    },
                    false
                );
            });
        })
    );
    console.log('Done');
    // await Promise.all(updates.map(update => {
    //     return limit(() => Search.updateDocument(update));
    // }));
    cursor.close();
}

update();