@@ -14,6 +14,7 @@ const fetch = require('node-fetch')
1414const { HttpsProxyAgent } = require ( 'https-proxy-agent' )
1515const windowStateKeeper = require ( 'electron-window-state' )
1616const express = require ( 'express' )
17+ const { globSync } = require ( 'glob' )
1718
1819const { prepareMangaModel, prepareMetadataModel } = require ( './modules/database' )
1920const { prepareTemplate } = require ( './modules/prepare_menu.js' )
@@ -674,16 +675,34 @@ ipcMain.handle('open-local-book', async (event, filepath) => {
674675
675676ipcMain . handle ( 'delete-local-book' , async ( event , filepath ) => {
676677 if ( filepath . startsWith ( setting . library ) ) {
677- await Manga . destroy ( { where : { filepath : filepath } } )
678678 try {
679- try {
679+ const stats = await fs . promises . stat ( filepath )
680+ if ( stats . isDirectory ( ) ) {
681+ const imageFiles = globSync ( '*.@(jpg|jpeg|png|webp|avif|gif)' , {
682+ cwd : filepath ,
683+ nocase : true ,
684+ absolute : true
685+ } )
686+
687+ for ( const imageFile of imageFiles ) {
688+ try {
689+ await shell . trashItem ( imageFile )
690+ } catch {
691+ await fs . promises . rm ( imageFile , { force : true } )
692+ }
693+ }
694+
695+ const remainingFiles = await fs . promises . readdir ( filepath )
696+ if ( remainingFiles . length === 0 ) {
697+ await shell . trashItem ( filepath )
698+ }
699+ } else {
680700 await shell . trashItem ( filepath )
681- } catch {
682- await fs . promises . rm ( filepath , { recursive : true , force : true } )
683701 }
684702 } catch ( e ) {
685703 sendMessageToWebContents ( `Delete ${ filepath } failed because ${ e } ` )
686704 }
705+ await Manga . destroy ( { where : { filepath : filepath } } )
687706 }
688707} )
689708
0 commit comments