hjyssg / ShiguReader

硬核宅宅资源管理器. Ultimate Manga Resource Manager
MIT License
397 stars 45 forks source link

snip #102

Closed hjyssg closed 3 years ago

hjyssg commented 3 years ago

const pathUtil = require("../pathUtil");
const {
        isExist
} = pathUtil;
const pfs = require('promise-fs');
const fs = require('fs');
const execa = require('execa');
const userConfig = global.requireUserConfig();
const isWindows = require('is-windows');
const express = require('express');
const router = express.Router();
const logger = require("../logger");
const path = require('path');

const util = global.requireUtil();
const { isImage, isCompress, isMusic, arraySlice, isDisplayableInOnebook } = util;

const sevenZipHelp = require("../sevenZipHelp");

function getReason(e){
    return e.stderr || e.message || e;
}

router.post('/api/renameFile', (req, res) => {
    const src = req.body && req.body.src;
    const dest = req.body && req.body.dest;

    if(!src || !dest){
        res.sendStatus(404);
        return;
    }

    (async () =>{
        try{
            let err = await pfs.rename(src, dest);

            if(err){ throw err; }

            logger.info(`[rename] ${src} to ${dest}`);
            res.sendStatus(200);
        }catch(err){
            console.error(err);
            res.status(500).send(getReason(err));
        }
    })();
});

router.post('/api/moveFile', (req, res) => {
    const src = req.body && req.body.src;
    const dest = req.body && req.body.dest;

    if(!src || !dest){
        res.sendStatus(404);
        return;
    }

    (async () =>{
        try{
            let err;
            if(!(await isExist(dest))){
                err = await pfs.mkdir(dest, {recursive: true});
            }

            if(err){ throw "fail to create dest folder";}

            const cmdStr = isWindows()? "move" : "mv";
            const {stdout, stderr} = await execa(cmdStr, [src, dest]);
            err = stderr;

            if(err){ throw err;}

            logger.info(`[MOVE] ${src} to ${dest}`);
            res.sendStatus(200);
        }catch(err){
            console.error(err);
            res.status(500).send(getReason(err));
        }
    })();
});

function deleteThing(src){
   if(userConfig.move_file_to_recyle){
        const trash = require('trash');
        await trash([src]);
        if(!(await isExist(src))){
            res.sendStatus(200);
            logger.info(`[DELETE] ${src}`);
        } else {
            //missing is delete
            res.sendStatus(200);
        }
    }else{
        const err = await pfs.unlink(src) 
        if (err){ throw err; }
    }
}

async function isSimpleFolder(src){
    let content_pathes = await pfs.readdir(src);
    const otherTypes = content_pathes.filter(e => !isDisplayableInOnebook(e));

    return otherTypes.length === 0;
}

const _folder_waring_ = "This folder is not a one-level img/music folder";
const file_occupy_warning = "File may be used by another process"

router.post('/api/deleteFile', async (req, res) => {
    const src = req.body && req.body.src;

    if(!src || !(await isExist(src))){
        res.sendStatus(404);
        return;
    }

    try{
        deleteThing(src);
        res.sendStatus(200);
        logger.info(`[DELETE] ${src}`);
    } catch(e) {
        console.error(e);
        res.status(500).send(file_occupy_warning);
    }
});

router.post('/api/deleteFolder', async (req, res) => {
    const src = req.body && req.body.src;

    if(!src || !(await isExist(src))){
        res.sendStatus(404);
        return;
    }

    if(!(await isSimpleFolder(src))){
        res.status(500).send(_folder_waring_);
        return;
    }

    //below is duplicate code as /api/deleteFile
    //need to improve
    try{
        deleteThing(src);
        res.sendStatus(200);
        logger.info(`[DELETE] ${src}`);
    } catch(e) {
        console.error(e);
        res.status(500).send(file_occupy_warning);
    }
});

router.post('/api/zipFolder', async (req, res) => {
    const src = req.body && req.body.src;

    if(!src || !(await isExist(src))){
        res.sendStatus(404);
        return;
    }

    if(! (await isSimpleFolder(src))){
        res.status(500).send(_folder_waring_);
        return;
    }

    try{
        let {stdout, stderr, resultZipPath} = await sevenZipHelp.zipOneFolder(src);
        if(stderr){
            throw stderr;
        }
        res.sendStatus(200);
        logger.info(`[zipFolder] ${src}`);
    } catch(e) {
        console.error(e);
        res.status(500).send("fail to zip");
    }
});

module.exports = router;
hjyssg commented 3 years ago
import _ from "underscore";
import 'whatwg-fetch';

const Sender = {};

_.resHandle = function (res) {
    if (res.status === 200 || res.status === 304) {
        return res.json();
    }

    //need to name res.res
    console.error('[failed]', res.status, res.statusText);
    res.failed = true;

    debugger
    res.text();
    return res;
};

// going to remove simplePost
// Sender.simplePost = function (api, body, callback) {
//     fetch(api, {
//         method: 'POST',
//         headers: {
//             Accept: 'application/json',
//             'Content-Type': 'application/json',
//         },
//         body: JSON.stringify(body)
//     })
//     .then(res => {
//         if(!(res.status === 200 || res.status === 304)){
//             res.failed = true;
//         }
//         callback(res);
//     });
// };

Sender.post = function (api, body, callback) {
    const pp = fetch(api, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(body)
    })
    .then(_.resHandle);

    if(callback){
        pp.then(callback);
    }else{
        return pp;
    }
};

Sender.get = function (api, callback) {
    const pp = fetch(api)
    .then(_.resHandle);

    if(callback){
        pp.then(callback);
    }else{
        return pp;
    }
};

//going to remove this lsdir
Sender.lsDir = function (body, callback) {
    Sender.post('/api/lsDir', body, callback);
};

export default Sender;