anotherim-desktop/plugins/openpgp/src/in_file_processor.vala

35 lines
1.3 KiB
Vala
Raw Normal View History

2017-10-15 22:23:51 +00:00
using Dino.Entities;
namespace Dino.Plugins.OpenPgp {
public class InFileProcessor : IncomingFileProcessor, Object {
2017-10-15 22:23:51 +00:00
public bool can_process(FileTransfer file_transfer) {
return file_transfer.file_name.has_suffix("pgp") || file_transfer.mime_type == "application/pgp-encrypted";
}
public void process(FileTransfer file_transfer) {
2017-10-29 14:15:28 +00:00
try {
uint8[] buf = new uint8[256];
Array<uint8> data = new Array<uint8>(false, true, 0);
size_t len = -1;
do {
len = file_transfer.input_stream.read(buf);
data.append_vals(buf, (uint) len);
} while(len > 0);
2017-10-15 22:23:51 +00:00
GPGHelper.DecryptedData clear_data = GPGHelper.decrypt_data(data.data);
file_transfer.input_stream = new MemoryInputStream.from_data(clear_data.data, GLib.free);
2017-10-29 14:15:28 +00:00
file_transfer.encryption = Encryption.PGP;
if (clear_data.filename != null && clear_data.filename != "") {
file_transfer.file_name = clear_data.filename;
} else if (file_transfer.file_name.has_suffix(".pgp")) {
2017-10-29 14:15:28 +00:00
file_transfer.file_name = file_transfer.file_name.substring(0, file_transfer.file_name.length - 4);
}
} catch (Error e) {
file_transfer.state = FileTransfer.State.FAILED;
2017-10-15 22:23:51 +00:00
}
}
}
}