const fs = require("fs");
const path = require("path");
const { download } = require("electron-dl");
const { dialog } = require("electron");
const notification = require("./notification");
const Caption = require("caption-core");

const rename = subtitles => {
  const { mainWindow } = global.windows;

  subtitles.map(subtitle =>
    fs.rename(subtitle.savePath, subtitle.filename, () => console.log("done")));

  if (subtitles.length > 0) {
    notification(`${subtitles.length} subtitles downloaded successfully!`);
  }

  mainWindow.webContents.send("download-complete", subtitles);
};

const multipleDownload = async files => {
  const { mainWindow } = global.windows;
  const items = [];

  try {
    for (let i = 0; i < files.length; i++) {
      const { file, subtitle } = files[i];
      const downloadLocation = path.dirname(file.path);
      const originalFileName = file.name;
      const subtitleFilename = originalFileName.replace(/\.[^/.]+$/, "");

      const options = {
        saveAs: false,
        directory: downloadLocation,
      };

      const dl = await download(mainWindow, subtitle.url, options);

      const downloadedItem = {
        savePath: dl.getSavePath(),
        filename: `${downloadLocation}/${subtitleFilename}.srt`,
      };

      items.push(downloadedItem);
    }
  } catch (error) {
    console.log(error);
  }

  rename(items);
};

const singleDownload = async item => {
  const hasExtension = item.name.includes(".srt");
  const filename = hasExtension ? item.name : `${item.name}.srt`;
  const { mainWindow } = global.windows;
  const saveToPath = await new Promise(resolve => {
    dialog.showSaveDialog(
      mainWindow,
      {
        title: "Download",
        defaultPath: filename,
      },
      resolve,
    );
  });

  if (!saveToPath) {
    return;
  }

  try {
    Caption.download(item, item.source, saveToPath)
      .then(() => {
        notification(`${item.name} is successfully downloaded!`);
        mainWindow.webContents.send("singleDownloadSuccesfull", item);
      })
      .catch(err => console.log("error", err));
  } catch (err) {
    console.log("err", err);
  }
};

module.exports = { multipleDownload, singleDownload };

@@ -3,99 +3,28 @@
 nst 
-fs = require(%22fs%22);%0Aconst path = require(%22path%22);%0Aconst %7B download %7D = require(%22electron-dl
+path = require(%22path
 %22);%0A
@@ -157,368 +157,8 @@
 );%0A%0A
-const rename = subtitles =%3E %7B%0A  const %7B mainWindow %7D = global.windows;%0A%0A  subtitles.map(subtitle =%3E%0A    fs.rename(subtitle.savePath, subtitle.filename, () =%3E console.log(%22done%22)));%0A%0A  if (subtitles.length %3E 0) %7B%0A    notification(%60$%7Bsubtitles.length%7D subtitles downloaded successfully!%60);%0A  %7D%0A%0A  mainWindow.webContents.send(%22download-complete%22, subtitles);%0A%7D;%0A%0A
 cons
@@ -192,24 +192,27 @@
 files =%3E %7B%0A 
+ //
  const %7B mai
@@ -242,28 +242,8 @@
 ows;
-%0A  const items = %5B%5D;
 %0A%0A  
@@ -519,338 +519,327 @@
     
-const options = %7B%0A        saveAs: false,%0A        directory: downloadLocation,
+Caption.download(
 %0A      
-%7D;%0A
+  %7B
 %0A      
-const dl = await download(mainWindow, subtitle.url, options);%0A%0A      const downloadedItem = %7B%0A        savePath: dl.getSavePath(),%0A        filename: %60$%7BdownloadLocation%7D/$%7BsubtitleFilename%7D.srt%60,%0A      %7D;%0A%0A      items.push(downloadedItem
+    downloadUrl: subtitle.url,%0A        %7D,%0A        %22opensubtitles%22,%0A        %60$%7BdownloadLocation%7D/$%7BsubtitleFilename%7D.srt%60,%0A      )%0A        .then(() =%3E %7B%0A          notification(%60$%7BoriginalFileName%7D is successfully downloaded!%60);%0A        %7D)%0A        .catch(err =%3E console.log(%22error%22, err)
 );%0A 
@@ -894,26 +894,8 @@
 %0A  %7D
-%0A%0A  rename(items);
 %0A%7D;%0A

Refactor file search and multi-download