File size: 2,071 Bytes
88ccbf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9765f38
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
// Just additional code for https://github.com/rkwyu/scribd-dl

import { exec } from 'child_process';  
import { promisify } from 'util';
import cors from 'cors'
import express from 'express'
import {config} from 'dotenv'
config()
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const execPromise = promisify(exec);

async function executeCommand(command) {
    try {
        const { stdout, stderr } = await execPromise(command); 
        if (stderr) {
            throw new Error(`Stderr: ${stderr}`);
        }

        return stdout;  
    } catch (error) {
        throw new Error(`Error executing command: ${error.message}`);
    }
}

function checkIfGenerated(output) {
    const logs = output.trim().split('\n');
    const lastLog = logs[logs.length - 1];   
    if (lastLog.includes('Generated: ')) {
        return { result: true, path: lastLog.split(" output/")[1] }
    } else {
        return { result: false, path: "" }
    }
}

async function main(url) {
    return new Promise(async (resolve, reject) => {
        try {
            const command = `npm start '${url}'`;  
            const output = await executeCommand(command); 
            resolve(checkIfGenerated(output));  
        } catch (error) {
            if(error.toString().includes("Unsupported UR")) {
                reject({error: "Not supported URL"})
            }else{
                reject({error: error})
            }
        }
    })

}

const app = express()
app.use(cors())

app.get("/", (req, res) => {
    res.send("/down?url=<scribd>")
})

app.all("/down", async(req,res) => {
    const {url} = req.query;
    if(!url) return res.json({error: "invalid url/nourl"})
    return res.json(await main(url))
})
app.use(express.static(path.join(__dirname, 'output')));

const port = process.env.SERVER_PORT || 7860
app.listen(port, () => {
    console.log("listening on port: " + port)
})

// await main(`https://www.scribd.com/doc/252120132/Rangkaian-Alarm-Anti-Maling`);