Skip to content

Commit

Permalink
Sanitize internal storage file names
Browse files Browse the repository at this point in the history
Sanitize an internally downloaded file's name during save process to prevent file transfer error for Windows users.
  • Loading branch information
Alphastaire committed Aug 12, 2024
1 parent c28d386 commit 1fc9997
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions libdino/src/service/file_manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class FileManager : StreamInteractionModule, Object {
return ret;
}

// Required to allow Windows users to receive files with illegal characters in name
private string sanitize_filename(string filename) {
GLib.Regex regex = new GLib.Regex("[<>:\"/\\|?*]");
return regex.replace(filename, -1, 0, "_");
}

public async void send_file(File file, Conversation conversation) {
FileTransfer file_transfer = new FileTransfer();
file_transfer.account = conversation.account;
Expand Down Expand Up @@ -243,7 +249,7 @@ public class FileManager : StreamInteractionModule, Object {
}

// Save file
string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name;
string filename = Random.next_int().to_string("%x") + "_" + sanitize_filename(file_transfer.file_name);
File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename));

OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
Expand Down Expand Up @@ -332,7 +338,7 @@ public class FileManager : StreamInteractionModule, Object {

private async void save_file(FileTransfer file_transfer) throws FileSendError {
try {
string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name;
string filename = Random.next_int().to_string("%x") + "_" + sanitize_filename(file_transfer.file_name);
File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename));
OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
yield os.splice_async(file_transfer.input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET);
Expand Down

0 comments on commit 1fc9997

Please sign in to comment.