File size: 3,746 Bytes
a4cf109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const { writeFileSync, copyFileSync, existsSync, mkdirSync, createWriteStream, constants } = require("fs");
const { join: pathJoin, extname, basename } = require("path");
const { v4 } = require("uuid");
const { domain, fetchOptions, additionalBookLocation } = require("./common");
const fetch = require("node-fetch");

module.exports = function (args, socket) {
    if (args.length < 1) {
        socket.write("ERR: No download link\n");
        return;
    }

    const xochitlFolder = "/home/root/.local/share/remarkable/xochitl/";
    const downloadURL = domain + args[0];
    console.log(downloadURL);

    fetch(downloadURL, fetchOptions).then((response) => {
        console.log("Redirected to:", response.url);

        let fileName;
        let fileExt;

        const disposition = response.headers.get('content-disposition');
        if (disposition) {
            const nameMatch = disposition.match(/filename="(.+)"/);
            if (nameMatch) {
                let name = nameMatch[1].replace(" (z-lib.org)", "");
                // Convert multi-byte chars to their true representation
                name = Buffer.from(name, "binary").toString("utf8");

                fileExt = extname(name);
                fileName = basename(name, fileExt);
                console.log(fileName, fileExt);
            }
        }

        if (!fileName || !fileExt) {
            socket.write("ERR: 2 No file\n");
            return;
        }

        const fileLength = parseInt(response.headers.get('content-length'));

        const uuid = v4();
        const tempFilePath = "/tmp/" + uuid;
        console.log(tempFilePath, fileLength);

        const fileStream = createWriteStream(tempFilePath);

        response.body.on("data", (chunk) => {
            fileStream.write(chunk, () => {
                socket.write("PROG:" + Math.floor(fileStream.bytesWritten / fileLength * 95).toString() + "\n");
                if (fileStream.bytesWritten == fileLength) {
                    fileStream.close();
                }
            });
        });

        response.body.on("error", (error) => {
            socket.write("ERR: 2 " + error + "\n");
            socket.end();
        });

        fileStream.on('close', () => {
            socket.write("DOWNLOAD DONE\n");

            if (fileExt == ".epub" || fileExt == ".pdf") {
                writeFileSync(xochitlFolder + uuid + ".metadata", JSON.stringify({
                    "deleted": false,
                    "lastModified": "1",
                    "lastOpenedPage": 0,
                    "metadatamodified": false,
                    "modified": false,
                    "parent": "",
                    "pinned": false,
                    "synced": false,
                    "type": "DocumentType",
                    "version": 1,
                    "visibleName": fileName
                }));

                copyFileSync(tempFilePath, pathJoin(xochitlFolder, uuid + fileExt), constants.COPYFILE_FICLONE);
                socket.write("COPIED XOCHITL\n");
            }

            if (additionalBookLocation) {
                if (!existsSync(additionalBookLocation)) {
                    mkdirSync(additionalBookLocation, { recursive: true });
                }
                copyFileSync(tempFilePath, pathJoin(additionalBookLocation, fileName + fileExt), constants.COPYFILE_FICLONE);
                socket.write("COPIED LOCAL\n");
            }
            socket.write("PROG:100\n");
            socket.end();
        });
    })
        .catch(err => {
            socket.write("ERR: 1 " + err + "\n");
            socket.end();
        });
}