upload usability
This commit is contained in:
parent
cc941d2b1a
commit
2541b3f31c
|
@ -15,7 +15,7 @@ import java.util.concurrent.LinkedBlockingDeque;
|
|||
|
||||
public class UploadQueue {
|
||||
public static final String OUTBOX_FOLDER = FileUtil.RUSEFI_SETTINGS_FOLDER + File.separator + "outbox";
|
||||
private static final LinkedBlockingDeque<String> queue = new LinkedBlockingDeque<>(128);
|
||||
private static final LinkedBlockingDeque<FileAndFolder> queue = new LinkedBlockingDeque<>(128);
|
||||
|
||||
private static boolean isStarted;
|
||||
|
||||
|
@ -51,8 +51,7 @@ public class UploadQueue {
|
|||
return;
|
||||
System.out.println(UploadQueue.class.getSimpleName() + " readOutbox " + file);
|
||||
try {
|
||||
String fileName = OUTBOX_FOLDER + File.separator + file;
|
||||
queue.put(fileName);
|
||||
queue.put(new FileAndFolder(OUTBOX_FOLDER, file));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -62,23 +61,23 @@ public class UploadQueue {
|
|||
|
||||
private static void uploadLoop() throws InterruptedException {
|
||||
while (true) {
|
||||
String fileName = queue.take();
|
||||
FileAndFolder file = queue.take();
|
||||
|
||||
UploadResult result = Online.upload(new File(fileName), AuthTokenPanel.getAuthToken());
|
||||
UploadResult result = Online.upload(new File(file.getFullName()), AuthTokenPanel.getAuthToken());
|
||||
System.out.println("isError " + result.isError());
|
||||
System.out.println("first " + result.getFirstMessage());
|
||||
if (result.isError() && result.getFirstMessage().contains("This file already exists")) {
|
||||
System.out.println(UploadQueue.class.getSimpleName() + " No need to re-try this one");
|
||||
delete(fileName);
|
||||
file.postUpload();
|
||||
// do not retry this error
|
||||
continue;
|
||||
}
|
||||
if (result.isError()) {
|
||||
System.out.println(UploadQueue.class.getSimpleName() + " Re-queueing " + fileName);
|
||||
queue.put(fileName);
|
||||
System.out.println(UploadQueue.class.getSimpleName() + " Re-queueing " + file.getFullName());
|
||||
queue.put(file);
|
||||
continue;
|
||||
}
|
||||
delete(fileName);
|
||||
file.postUpload();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,11 +96,45 @@ public class UploadQueue {
|
|||
msq.bibliography.setTuneComment("Auto-saved");
|
||||
try {
|
||||
new File(OUTBOX_FOLDER).mkdirs();
|
||||
String fileName = OUTBOX_FOLDER + File.separator + System.currentTimeMillis() + ".msq";
|
||||
msq.writeXmlFile(fileName);
|
||||
queue.put(fileName);
|
||||
String fileName = System.currentTimeMillis() + ".msq";
|
||||
String fullFileName = OUTBOX_FOLDER + File.separator + fileName;
|
||||
msq.writeXmlFile(fullFileName);
|
||||
queue.put(new FileAndFolder(OUTBOX_FOLDER, fileName));
|
||||
} catch (InterruptedException | JAXBException | IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static class FileAndFolder {
|
||||
private static final boolean DEBUG_SAVE_UPLOADED = false;
|
||||
private final String folder;
|
||||
private final String file;
|
||||
|
||||
public FileAndFolder(String folder, String file) {
|
||||
this.folder = folder;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
public String getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return folder + File.separator + file;
|
||||
}
|
||||
|
||||
public void postUpload() {
|
||||
if (DEBUG_SAVE_UPLOADED) {
|
||||
String uploadedDir = folder + File.separator + "uploaded";
|
||||
new File(uploadedDir).mkdirs();
|
||||
new File(getFullName()).renameTo(new File(uploadedDir + File.separator + file));
|
||||
} else {
|
||||
delete(getFullName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue