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 and potentially Linux users.
  • Loading branch information
Alphastaire authored and mxlgv committed Aug 18, 2024
1 parent 80af067 commit 91e7435
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions libdino/src/service/file_manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public class FileManager : StreamInteractionModule, Object {
return ret;
}

private string sanitize_filename(string filename) {
#if _WIN32
GLib.Regex regex = new GLib.Regex("[<>:\"/\\|?*]");
#else
GLib.Regex regex = new GLib.Regex("[/]");
#endif
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 +252,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 +341,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 91e7435

Please sign in to comment.