def index(request): """ Main page to upload documents :param request: wsgi request :return: render upload page """ syncdb.sync_files() return render(request, 'upload.html')
def download_split_tcp(request): """ Method for splitting file across different connected nodes. Purpose is to allow a master node which can be accessed by the hosting system, to upload and download files that are split up, and encrypted with different keys for each piece. :param request: wsgi request :return: redirect """ filename = request.GET.get('filename', 'test') try: fileobj = SplitFile.objects.get(name=filename) file1 = '/files/first_' + filename file2 = '/files/second_' + filename client.client_send(fileobj.node1, fileobj.port1, file1) client.client_send(fileobj.node2, fileobj.port2, file2) filenames = [file1, file2] with open('/files/' + filename, 'w') as outfile: for fname in filenames: with open(fname) as infile: for line in infile: outfile.write(line) for fname in filenames: os.remove(fname) added_files, removed_files = syncdb.sync_files() return render(request, 'sync_results.html', { 'added_files': added_files, 'removed_files': removed_files }) except: return HttpResponse('No such file')
def sync_files(request): """ Method to manually sync any files to the database. Can add files to /files/ directory and hit sync and it will update database. Can also remove files from directory and sync will update it. :param request: wsgi request :return: render sync results page with files added and removed """ added_files, removed_files = syncdb.sync_files() return render(request, 'sync_results.html', { 'added_files': added_files, 'removed_files': removed_files })
def download_single_file(request): """ Calls tcp client connection to the requested host and port for downloading a specific file :param request: wsgi request :return: files added and removed from sync method """ filename = request.GET.get('filename') ip = request.GET.get('ip') port = request.GET.get('port') client.client_send(ip, port, filename) added_files, removed_files = syncdb.sync_files() return added_files, removed_files
def upload_file(request): """ Upload method to upload files to system, intakes a file and splits it up into two pieces, then sends each piece to each of the filenodes that are on the system as :param request: wsgi request :return: None """ filenodes = FileNodes.objects.get(id=1) if request.method != 'POST': return HttpResponseBadRequest('Only POST requests are allowed') request_file = request.FILES['myfile'] with open('/files/%s' % request_file.name, 'wb+') as dest: for chunk in request_file.chunks(): dest.write(chunk) size = os.path.getsize('/files/' + request_file.name) with open('/files/%s' % request_file.name) as source: filenames = [ '/files/' + 'first_' + request_file.name, '/files/' + 'second_' + request_file.name ] with open(filenames[0], 'wb') as dest: for data in source.readlines(size / 2): dest.write(data) with open(filenames[1], 'wb') as dest: for data in source.readlines(size / 2): dest.write(data) client2.client_send(filenodes.ip_address1, filenodes.port1, filenames[0]) client2.client_send(filenodes.ip_address2, filenodes.port2, filenames[1]) for file in filenames: os.remove(file) added_files, removed_files = syncdb.sync_files() split_file = SplitFile() split_file.name = request_file.name split_file.node1 = filenodes.ip_address1 split_file.node2 = filenodes.ip_address2 split_file.port1 = filenodes.port1 split_file.port2 = filenodes.port2 split_file.save() print added_files print removed_files