Skip to content

Commit

Permalink
sending file data without reading/writing to file
Browse files Browse the repository at this point in the history
-automatically accepts .png files <=1MiB as data
  • Loading branch information
notsecure committed May 27, 2014
1 parent 82ee776 commit 7331aad
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 21 deletions.
29 changes: 29 additions & 0 deletions src/core/FileTransfer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace Venom {
_bytes_processed = value;
}
}
public bool isfile { get; set; }
public uint8[] data { get; set; }
public string name { get; set; }
public string path { get; set; }
public DateTime time_sent { get; set; }
Expand All @@ -73,10 +75,37 @@ namespace Venom {
this.send_receive = send_receive;
this.file_size = file_size;
this.name = name;
this.isfile = true;
this.path = path;
this.time_sent = new DateTime.now_local();
this.status = FileTransferStatus.PENDING;
this.bytes_processed = 0;
}

public FileTransfer.senddata(Contact friend, string name, uint8[] data) {
this.friend = friend;
this.direction = FileTransferDirection.OUTGOING;
this.send_receive = 0;
this.file_size = data.length;
this.name = name;
this.isfile = false;
this.data = data;
this.time_sent = new DateTime.now_local();
this.status = FileTransferStatus.PENDING;
this.bytes_processed = 0;
}

public FileTransfer.recvdata(Contact friend, string name, uint64 file_size) {
this.friend = friend;
this.direction = FileTransferDirection.INCOMING;
this.send_receive = 1;
this.file_size = file_size;
this.name = name;
this.isfile = false;
this.data = {};
this.time_sent = new DateTime.now_local();
this.status = FileTransferStatus.PENDING;
this.bytes_processed = 0;
}
}
}
80 changes: 59 additions & 21 deletions src/ui/ContactListWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,24 @@ namespace Venom {
private void on_file_sendrequest(int friendnumber, uint8 filenumber, uint64 filesize,string filename) {
stdout.printf ("received file send request friend: %i filenumber: %i filename: %s \n",friendnumber,filenumber,filename );
Contact contact = session.get_contact_list()[friendnumber];
FileTransfer ft = new FileTransfer(contact, FileTransferDirection.INCOMING, filesize, filename, null);
FileTransfer ft;
if(filename.has_suffix(".png") && filesize <= 0x100000) {
ft = new FileTransfer.recvdata(contact, filename, filesize);
} else {
ft = new FileTransfer(contact, FileTransferDirection.INCOMING, filesize, filename, null);
}

ft.filenumber = filenumber;
GLib.HashTable<uint8,FileTransfer> transfers = session.get_contact_list()[friendnumber].get_filetransfers();
transfers[filenumber] = ft;
ConversationWidget w = conversation_widgets[friendnumber];
w.on_incoming_filetransfer(ft);

if(!ft.isfile) {
session.accept_file(friendnumber, filenumber);
ft.status = FileTransferStatus.IN_PROGRESS;
}

this.set_urgency();
}

Expand All @@ -802,12 +814,25 @@ namespace Venom {
stderr.printf("Trying to send unknown file");
return;
}
File file = File.new_for_path(ft.path);

GLib.FileInputStream file_stream = null;
File file = null;

if(ft.isfile) {
file = File.new_for_path(ft.path);
}

try {
file_stream = file.read();
var file_info = file.query_info ("*", FileQueryInfoFlags.NONE);
uint64 file_size = file_info.get_size();
uint64 file_size;

if(ft.isfile) {
file_stream = file.read();
var file_info = file.query_info ("*", FileQueryInfoFlags.NONE);
file_size = file_info.get_size();
} else {
file_size = ft.file_size;
}

uint64 remaining_bytes_to_send = file_size - ft.bytes_processed;
uint8[] bytes = new uint8[chunk_size];
bool read_more = true;
Expand All @@ -820,14 +845,19 @@ namespace Venom {
Thread.usleep(1000);
continue;
}

if(remaining_bytes_to_send < chunk_size) {
chunk_size = (int) remaining_bytes_to_send;
bytes = new uint8[chunk_size];
}
if(read_more) {
size_t res = file_stream.read(bytes);
if(res != chunk_size) {
stderr.printf("Read incorrect number of bytes from file\n");
if(ft.isfile) {
size_t res = file_stream.read(bytes);
if(res != chunk_size) {
stderr.printf("Read incorrect number of bytes from file\n");
}
} else {
Memory.copy(bytes, (uint8*)ft.data + ft.bytes_processed, chunk_size);
}
}
int res = session.send_file_data(friendnumber,filenumber,bytes);
Expand Down Expand Up @@ -903,20 +933,28 @@ namespace Venom {
session.reject_file(friendnumber,filenumber);
return;
}
string path = ft.path;
File file = File.new_for_path(path);
try{
if(!file.query_exists())
file.create(FileCreateFlags.NONE);
FileOutputStream fos = file.append_to(FileCreateFlags.NONE);
size_t bytes_written;
fos.write_all(data,out bytes_written);
ft.bytes_processed += bytes_written;
fos.close();
} catch (Error e){
stderr.printf("Error while trying to write data to file\n");
ft.status = FileTransferStatus.RECEIVING_FAILED;

if(ft.isfile) {
string path = ft.path;
File file = File.new_for_path(path);
try{
if(!file.query_exists())
file.create(FileCreateFlags.NONE);
FileOutputStream fos = file.append_to(FileCreateFlags.NONE);
size_t bytes_written;
fos.write_all(data,out bytes_written);
ft.bytes_processed += bytes_written;
fos.close();
} catch (Error e){
stderr.printf("Error while trying to write data to file\n");
ft.status = FileTransferStatus.RECEIVING_FAILED;
}
} else {
ByteArray buffer = new ByteArray.take(ft.data);
buffer.append(data);
ft.data = buffer.data;
}

}

private ConversationWidget? open_conversation_with(Contact c) {
Expand Down

0 comments on commit 7331aad

Please sign in to comment.