-
Notifications
You must be signed in to change notification settings - Fork 32
/
FileService.java
153 lines (128 loc) · 5.35 KB
/
FileService.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2019, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixed.service.file;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.comixed.adaptors.archive.ArchiveAdaptor;
import org.comixed.adaptors.archive.ArchiveAdaptorException;
import org.comixed.handlers.ComicFileHandler;
import org.comixed.handlers.ComicFileHandlerException;
import org.comixed.model.file.FileDetails;
import org.comixed.model.library.Comic;
import org.comixed.model.tasks.TaskType;
import org.comixed.repositories.library.ComicRepository;
import org.comixed.service.task.TaskService;
import org.comixed.task.model.QueueComicsWorkerTask;
import org.comixed.task.runner.Worker;
import org.comixed.utils.ComicFileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FileService {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired private ComicFileHandler comicFileHandler;
@Autowired private ComicRepository comicRepository;
@Autowired private Worker worker;
@Autowired private ObjectFactory<QueueComicsWorkerTask> taskFactory;
@Autowired private TaskService taskService;
private int requestId = 0;
public byte[] getImportFileCover(final String comicArchive)
throws ComicFileHandlerException, ArchiveAdaptorException {
this.logger.debug("Getting first image from archive: {}", comicArchive);
byte[] result = null;
final ArchiveAdaptor archiveAdaptor = this.comicFileHandler.getArchiveAdaptorFor(comicArchive);
if (archiveAdaptor == null) {
this.logger.debug("No archive adaptor available");
return null;
}
final String coverFile = archiveAdaptor.getFirstImageFileName(comicArchive);
if (coverFile == null) {
this.logger.debug("Archive contains no images");
return null;
}
this.logger.debug("Fetching image content: entry={}", coverFile);
result = archiveAdaptor.loadSingleFile(comicArchive, coverFile);
this.logger.debug("Returning {} bytes", result == null ? 0 : result.length);
return result;
}
public List<FileDetails> getAllComicsUnder(final String rootDirectory) throws IOException {
this.logger.debug("Getting comics below root: {}", rootDirectory);
final File rootFile = new File(rootDirectory);
final List<FileDetails> result = new ArrayList<>();
if (rootFile.exists()) {
if (rootFile.isDirectory()) {
this.loadFilesUnder(result, rootFile);
} else {
this.logger.debug("Cannot process a file");
}
} else {
this.logger.debug("Directory not found");
}
return result;
}
private void loadFilesUnder(final List<FileDetails> files, final File directory)
throws IOException {
this.logger.debug("Loading files in directory: {}", directory);
if (directory.listFiles() != null) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
this.loadFilesUnder(files, file);
} else {
if (ComicFileUtils.isComicFile(file)) {
final String filePath = file.getCanonicalPath();
final long fileSize = file.length();
final Comic comic = this.comicRepository.findByFilename(filePath);
if (comic != null) {
this.logger.debug("File already in the library: id={}", comic.getId());
} else {
this.logger.debug("Adding file: {} ({} bytes)", filePath, fileSize);
files.add(new FileDetails(filePath, fileSize));
}
}
}
}
}
}
public int getImportStatus() throws InterruptedException {
return this.taskService.getTaskCount(TaskType.ADD_COMIC)
+ this.taskService.getTaskCount(TaskType.PROCESS_COMIC);
}
public int importComicFiles(
final String[] filenames, final boolean deleteBlockedPages, final boolean ignoreMetadata)
throws UnsupportedEncodingException {
this.logger.debug(
"Preparing to import {} comic files: delete blocked pages={} ignore metadata={}",
filenames.length,
deleteBlockedPages ? "Yes" : "No",
ignoreMetadata ? "Yes" : "No");
QueueComicsWorkerTask task = this.taskFactory.getObject();
task.setFilenames(Arrays.asList(filenames));
task.setDeleteBlockedPages(deleteBlockedPages);
task.setIgnoreMetadata(ignoreMetadata);
this.logger.debug("Adding import task to queue");
this.worker.addTasksToQueue(task);
return filenames.length;
}
}