def verify(uri): for path in walk(uri): file_hash = os.path.basename( os.path.dirname(path)) + os.path.basename(path) new_file_hash = calculate_hash_for_file(path) print(('Verification %s %s' % (path, 'OK' if file_hash == new_file_hash else 'FAIL')))
def test_calculate_hash(self): """Test accurate hash generated from file""" data = ( b'QlJBTlQgSVMgU1VQRVIgQVdFU09NRSBDT09MIEFORCBTSElULiBTTFVHUyBBUkUg' b'Q1VURQ==') expected_hash = '03439afa9d61f99f35d936b01ea8b4982ab247a0' t = tempfile.NamedTemporaryFile(delete=False) t.write(data) t.close() ret = utils.calculate_hash_for_file(t.name) self.assertEqual(ret, expected_hash)
def test_calculate_hash(self): """Test accurate hash generated from file""" data = ( b'QlJBTlQgSVMgU1VQRVIgQVdFU09NRSBDT09MIEFORCBTSElULiBTTFVHUyBBUkUg' b'Q1VURQ==' ) expected_hash = '03439afa9d61f99f35d936b01ea8b4982ab247a0' t = tempfile.NamedTemporaryFile(delete=False) t.write(data) t.close() ret = utils.calculate_hash_for_file(t.name) self.assertEqual(ret, expected_hash)
def post(self, request, project_name): project = get_object_or_404(Project, pk=project_name) if ('path_id' not in request.POST and 'path' not in request.POST) or 'file' not in request.FILES: return Response('Malformed request, needs to contain filename and file fields', status=400) if 'path' in request.POST: path = Path.objects.get_or_create(project, request.POST['path']) else: path_id = request.POST['path_id'] path = get_object_or_404(Path, project=project, pk=path_id) file_obj = request.FILES['file'] filename = file_obj.name #Write file to temporary location with tempfile.NamedTemporaryFile(delete=False) as destination: for chunk in file_obj.chunks(): destination.write(chunk) destination.flush() hash = calculate_hash_for_file(destination.name) files_dir = '/tmp/damn/files' if not os.path.exists(files_dir): os.makedirs(files_dir) new_path = os.path.join(files_dir, hash) os.rename(destination.name, new_path) analyzer = Analyzer() try: file_descr = analyzer.analyze_file(new_path) except AnalyzerException as ae: file_descr = FileDescription(file=FileId()) file_descr.file.hash = hash file_ref = FileReference.objects.update_or_create(request.user, project, path, filename, file_descr) return Response(serializers.FileReferenceSerializer(file_ref, context={'request': request}).data)
(path, 'OK' if file_hash == new_file_hash else 'FAIL'))) if __name__ == '__main__': test_files_dir = os.path.join(os.path.dirname(__file__), '../../../peragro-test-files') for path in walk(test_files_dir): file_hash, block_hashes = block_hashes_for_file(path) block_hashes_to_file(file_hash, block_hashes, '/tmp/files/' + hash_to_dir(file_hash)) blocks_for_file(path, '/tmp/blocks') statistics('/tmp/files/') for path in walk(test_files_dir): file_hash = calculate_hash_for_file(path) block_hashes = block_hashes_from_file('/tmp/files/' + hash_to_dir(file_hash)) new_path = '/tmp/out/' + os.path.basename(path) blocks_to_file('/tmp/blocks', block_hashes, new_path) new_file_hash = calculate_hash_for_file(new_path) print(('Verification %s' % ('OK' if file_hash == new_file_hash else 'FAIL'))) verify('/tmp/blocks') block_hashes = [ 'b499ca988473a4abc0461717cc8d4ce8753aa6d9', # exists 'b497ca988473a4abc0461717cc8d4ce8753aa6d9', 'b497ca988473a4abc0461717cc8d4ce8753aa6d9',