Skip to content

Commit

Permalink
Merge pull request #27 from Ogefest/fix/invalid-filenames
Browse files Browse the repository at this point in the history
Fix/invalid filenames
  • Loading branch information
Ogefest authored Apr 6, 2021
2 parents 6bdd2aa + b167d3c commit 5a9e89b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/notepack/SetNameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import notepack.app.domain.App;
import notepack.app.domain.Note;
import notepack.app.domain.PopupController;
import notepack.app.storage.Validator;
import notepack.app.task.NoteRename;
import notepack.app.task.NoteSave;
import notepack.app.task.WorkspaceRefresh;
Expand Down Expand Up @@ -69,6 +71,15 @@ void onSaveBtn(ActionEvent event) {

}

if (!Validator.isNameValid(name)) {
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
errorAlert.setHeaderText(null);
errorAlert.setContentText("Invalid filename");
errorAlert.showAndWait();

return;
}

if (note.getPath() != null) {
app.addTask(new NoteRename(note, note.getWorkspace().getStorage().getBasePath() + File.separator + name));
} else {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/notepack/app/storage/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package notepack.app.storage;

public class Validator {

public static boolean isNameValid(String name) {

char[] invalid = {'<', '>', ':', '"', '/', '\\', '|', '?', '*'};
for (char ch : invalid) {
if (name.indexOf(ch) > -1) {
return false;
}
}

return true;
}

}
13 changes: 10 additions & 3 deletions src/main/java/notepack/app/task/NoteRename.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
import notepack.app.domain.Note;
import notepack.app.domain.Task;
import notepack.app.listener.NoteListener;
import notepack.app.storage.Validator;

public class NoteRename extends BaseTask implements Task, TypeNote {

public class NoteRename implements Task,TypeNote {

private Note note;
private String newPath;

public NoteRename(Note n, String newPath) {
this.note = n;
this.newPath = newPath;
}

@Override
public void backgroundWork() {

if (!Validator.isNameValid(newPath)) {
addTaskToQueue(new ShowUserMessage("Invalid note name", ShowUserMessage.TYPE.ERROR));
return;
}

String oldNoteIdent = note.getIdent();
note.getStorage().rename(note.getPath(), newPath);
note.setPath(newPath);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/notepack/app/task/NoteSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import notepack.app.domain.Task;
import notepack.app.domain.exception.MessageError;
import notepack.app.listener.NoteListener;
import notepack.app.storage.Validator;
import notepack.gui.TaskUtil;

public class NoteSave extends BaseTask implements Task, TypeNote,TypeGui {
Expand All @@ -19,7 +20,14 @@ public NoteSave(Note note) {

@Override
public void backgroundWork() throws MessageError {

if (note.getPath() != null) {

if (!Validator.isNameValid(note.getPath())) {
addTaskToQueue(new ShowUserMessage("Invalid note name", ShowUserMessage.TYPE.ERROR));
return;
}

note.saveToStorage();
addTaskToQueue(new NoteMarkAsSaved(note));
addTaskToQueue(new WorkspaceRefresh(note.getWorkspace()));
Expand Down

0 comments on commit 5a9e89b

Please sign in to comment.