def chunkcallback(self, chunk_size, fnamestr): order_and_data = [0, ''] expected_threshold = [chunk_size] parsed_form = parse_chunks(fnamestr) if parsed_form: fname, framenum, chunks, user_or_cache = parsed_form video_name = fname fname = fname + '.' + framenum video_dirname = 'video-' + video_name dirname = video_dirname + '/' + fname + '.dir' print "HIHIHIHI" try: os.mkdir(video_dirname) except: pass try: os.mkdir(dirname) except: pass def helper(data): if order_and_data[0] >= len(chunks): pass else: filestr = dirname + '/' + fname + '.' + str(chunks[order_and_data[0]]).zfill(2) + '_40.chunk' total_curr_bytes = len(order_and_data[1]) + len(data) extra_bytes = total_curr_bytes - expected_threshold[0] if extra_bytes < 0: # expecting more tcp packets order_and_data[1] = ''.join([order_and_data[1], data]) else: trunc_data = data if extra_bytes > 0: trunc_data = data[:len(data)-extra_bytes] datastring = ''.join([order_and_data[1], trunc_data]) if DEBUGGING_MSG: outputStr = "Writing %s (actual: %d, expected: %d, totalCurrBytes: %d).\n" % \ (filestr, len(datastring), chunk_size, total_curr_bytes) sys.stdout.write(outputStr) sys.stdout.flush() file_to_write = open(filestr, 'wb') file_to_write.write(datastring) file_to_write.close() # reset order_and_data[1] = '' # new data string order_and_data[0] += 1 # new file extension if extra_bytes > 0: order_and_data[1] = data[len(data)-extra_bytes:] if len(chunks) == 0: # If empty chunks, just do nothing with received data from RETR. return lambda data : None else: # Save received chunks to files inside directory. return helper
def ftp_RETR(self, file): """Retrieve the specified file (transfer from the server to the client). Accepts filestrings of the form: chunk-<filename>.<ext>&<framenum>/<chunknum> file-<filename> """ parsedform = parse_chunks(file) if parsedform: filename, framenum, chunks, user_or_cache = parsedform each_chunk_size = self.movie_LUT.chunk_size_lookup(filename) ## Check ID & Log appropriately if user_or_cache == 1: log_load('user', int(each_chunk_size) * len(chunks)) else: log_load('cache', int(each_chunk_size) * len(chunks)) try: # filename should be prefixed by "file-" in order to be valid. # frame number is expected to exist for this cache. chunksdir = 'video-' + filename framedir = filename + '.' + framenum + '.dir' path = self.movies_path + '/' + chunksdir + '/' + framedir # get chunks list and open up all files files = self.get_chunk_files(path, chunks) # if DEBUGGING_MSG: # print "chunks requested:", chunks # print 'chunksdir', chunksdir # print 'framedir', framedir # print 'path', path except OSError, err: why = ftpserver._strerror(err) self.respond('550 %s.' % why) producer = self.chunkproducer(files, self._current_type) self.push_dtp_data(producer, isproducer=True, file=None, cmd="RETR") return