def write_chunks(): for chunk in FileChunk.objects(files_id=stored_file.id).order_by('+n'): if chunk.n >= start_chunk_number and chunk.n <= end_chunk_number: start = 0 end = stored_file.chunkSize if chunk.n == start_chunk_number: start = start_index if chunk.n == end_chunk_number: end = end_index yield chunk.data[start:end]
def add_chunk_to_file(file_id, result_id=None): """Add content to a file (chunk by chunk).""" stored_file = StoredFile.objects(id=ObjectId(file_id)).first() num_of_chunks = len(FileChunk.objects(files_id=stored_file.id)) chunk = FileChunk() chunk.files_id = stored_file.id chunk.n = num_of_chunks chunk.data = request.data chunk.save() stored_file.length += len(request.data) stored_file.save() return JsonResponse(stored_file)
def set_file_content(file_id): """Upload all the content at once (for small files).""" stored_file = StoredFile.objects(id=ObjectId(file_id)).first() data = request.data stored_file.md5 = md5(data).hexdigest() stored_file.length = len(data) num_of_chunks = len(data) / 262144 if (len(data) % 262144) > 0: num_of_chunks += 1 for i in range(num_of_chunks): chunk = FileChunk() chunk.files_id = stored_file.id chunk.n = i chunk.data = data[i * 262144:(i + 1) * 262144] chunk.save() stored_file.save() return JsonResponse(stored_file)
def write_chunks(): for chunk in FileChunk.objects(files_id=stored_file.id).order_by('+n'): yield chunk.data