Skip to content

Commit

Permalink
- bug fix in batch download (empty list)
Browse files Browse the repository at this point in the history
- bug fix in batch download (immutable list)
- bug fix in batch download (order in queue)
  • Loading branch information
BeanVortex committed May 17, 2023
1 parent 04d6a19 commit d9d013b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
17 changes: 12 additions & 5 deletions src/main/java/ir/darkdeveloper/bitkip/controllers/BatchList.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -94,7 +95,7 @@ private void onCancel() {
@FXML
private void onAdd() {
var links = linkTableUtils.getLinks();
var downloads = createDownloads(links);
var downloads = new ArrayList<>(createDownloads(links));
Collections.reverse(downloads);
DownloadsRepo.insertDownloads(downloads);
mainTableUtils.addRows(downloads);
Expand All @@ -103,7 +104,9 @@ private void onAdd() {


private List<DownloadModel> createDownloads(List<LinkModel> links) {
return links.stream().map(lm -> {
var list = new ArrayList<DownloadModel>();
for (int i = 0; i < links.size(); i++) {
var lm = links.get(i);
var dm = new DownloadModel();
dm.setUrl(lm.getUrl());
var fileName = lm.getName();
Expand All @@ -117,12 +120,16 @@ private List<DownloadModel> createDownloads(List<LinkModel> links) {
dm.setSize(lm.getSize());
dm.setChunks(lm.getChunks());
dm.setAddDate(LocalDateTime.now());
dm.setAddToQueueDate(LocalDateTime.now());
if (i == 0)
dm.setAddToQueueDate(LocalDateTime.now());
else
dm.setAddToQueueDate(list.get(i-1).getAddToQueueDate().plusSeconds(1));
dm.setResumable(lm.getResumeable());
dm.setQueues(new CopyOnWriteArrayList<>(lm.getQueues()));
dm.setDownloadStatus(DownloadStatus.Paused);
dm.setAgent(lm.getAgent());
return dm;
}).toList();
list.add(dm);
}
return list;
}
}
4 changes: 2 additions & 2 deletions src/main/java/ir/darkdeveloper/bitkip/repo/DownloadsRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static List<DownloadModel> findByURL(String url) {
return fetchDownloads(sql, false);
}

public static void updateDownloadQueue(int download_id, int queue_id) {
public static void updateDownloadQueue(int download_id, int queue_id, String addToQueue) {
var colQueueCount = "queue_count";
var queueCountSql = """
SELECT count(*) AS %s FROM %s d
Expand Down Expand Up @@ -205,7 +205,7 @@ SELECT count(*) AS %s FROM %s d
UPDATE %s SET %s = "%s" WHERE %s = %d;
"""
.formatted(DOWNLOADS_TABLE_NAME,
COL_ADD_TO_QUEUE_DATE, LocalDateTime.now(),
COL_ADD_TO_QUEUE_DATE, addToQueue,
COL_ID, download_id);

try (var con = DatabaseHelper.openConnection();
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/ir/darkdeveloper/bitkip/servlets/BatchServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.application.Platform;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static ir.darkdeveloper.bitkip.config.AppConfigs.log;
Expand All @@ -26,6 +27,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
try {
var urlModel = mapper.readValue(req.getReader(), BatchURLModel.class);
var links = convertToLinks(urlModel);
if (links.isEmpty())
throw new IOException("Empty data sent by extension");
Platform.runLater(() -> FxUtils.newBatchListStage(links));
} catch (IOException e) {
try {
Expand All @@ -38,19 +41,22 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
}

private List<LinkModel> convertToLinks(BatchURLModel urlModel) {
var threads = InputValidations.maxChunks();
var links = urlModel.links().stream().map(s -> new LinkModel(s, threads)).toList();
var links = urlModel.links();
if (links == null || links.isEmpty())
return Collections.emptyList();
var chunks = InputValidations.maxChunks();
var allDownloadsQueue = QueuesRepo.findByName(ALL_DOWNLOADS_QUEUE, false);
var firstUrl = links.get(0).getUrl();
var firstUrl = links.get(0);
var connection = NewDownloadUtils.connect(firstUrl, 3000, 3000);
var firstFileName = NewDownloadUtils.extractFileName(firstUrl, connection);
var secondaryQueue = BatchDownload.getSecondaryQueueByFileName(firstFileName);
var path = NewDownloadUtils.determineLocation(firstFileName);
links.forEach(lm -> {
return urlModel.links().stream().map(s -> {
var lm = new LinkModel(s, chunks);
lm.getQueues().add(allDownloadsQueue);
lm.getQueues().add(secondaryQueue);
lm.setPath(path);
});
return links;
return lm;
}).toList();
}
}
14 changes: 9 additions & 5 deletions src/main/java/ir/darkdeveloper/bitkip/utils/MenuUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javafx.scene.paint.Paint;

import java.io.File;
import java.time.LocalDateTime;
import java.util.*;

import static ir.darkdeveloper.bitkip.config.AppConfigs.*;
Expand Down Expand Up @@ -261,7 +262,7 @@ private static void initQueueMenuList(Menu addToQueueMenu, Menu startQueueMenu,
public static void deleteFromQueue() {
var notObserved = new ArrayList<>(mainTableUtils.getSelected());
var moveFiles = FxUtils.askToMoveFiles(notObserved, null);
for (DownloadModel dm : notObserved) {
for (var dm : notObserved) {
mainTableUtils.remove(dm);
dm.getQueues()
.stream()
Expand All @@ -285,7 +286,8 @@ public static void initAddToQueueMenu(Menu addToQueueMenu, LinkedHashMap<MenuIte
var qm = addToQueueItems.get(menuItem);
var notObserved = new ArrayList<>(mainTableUtils.getSelected());
var moveFiles = FxUtils.askToMoveFiles(notObserved, qm);
notObserved.forEach(dm -> {
for (int i = 0; i < notObserved.size(); i++) {
var dm = notObserved.get(i);
if (dm.getQueues().contains(qm))
return;
if (staticQueueNames.stream().noneMatch(s -> dm.getQueues().get(0).getName().equals(s)))
Expand All @@ -298,9 +300,11 @@ public static void initAddToQueueMenu(Menu addToQueueMenu, LinkedHashMap<MenuIte
newFilePath = queuesPath + qm.getName() + File.separator + dm.getName();
IOUtils.moveDownloadFilesFiles(dm, newFilePath);
}

DownloadsRepo.updateDownloadQueue(dm.getId(), qm.getId());
});
var addToQueueDate = LocalDateTime.now();
if (i != 0)
addToQueueDate = notObserved.get(i - 1).getAddToQueueDate().plusSeconds(1);
DownloadsRepo.updateDownloadQueue(dm.getId(), qm.getId(), addToQueueDate.toString());
}
}));
}

Expand Down

0 comments on commit d9d013b

Please sign in to comment.