def fetch_file(path, type, friendly): # TODO: Need protection against hack such as ../ buffer = ds.open(path) if buffer: file = buffer.read() # TODO: Maybe transform the httpresponse to streaminghttpresponse in case the graph is really large and to improve efficiency response = HttpResponse(file, content_type=type) # Get filename. if friendly: path = path.rpartition('/')[0] + '/' + ps.get_file_by_name(path).friendly_name # Construct and send response response['Content-Disposition'] = 'attachment; filename="' + path + '"' return response else: return HttpResponseNotFound('<h1>404 : ' + path + ' not found</h1>')
def open(self, mode=None): if mode is not None: self.mode = mode self.file_handle = ds.open(self.path, self.mode) return self.file_handle
def file_preview(request, file = None): app_access = __check_app_access() if app_access is not None: return app_access ## Authentication. authed_user = auth.get_current_user() if authed_user is None: authed_user_nick = 'Guest' else: authed_user_nick = authed_user.nickname() ## Graph visualisation. # Replaced by a spinning canvas on the clientside # if not ds.check_exists(GRAPH_BUCKET + '/' + file_name_without_extension + '.png', None): # file_name_without_extension = None #TODO: Might need to be simplified or moved to a function in fileinfo # TODO the folowing should be replaced by a method in the APIDatastore file_info = ps.get_file_by_name(DATA_BUCKET + '/' + file) undo_uri = None if file_info is not None: prev_file = ps.get_file_by_key(file_info.prev_file_key) if prev_file is not None: undo_uri = prev_file.file_name.rpartition(DATA_BUCKET + '/')[2] lst = ds.list(DATA_BUCKET) current_file = None for temp_file in lst: temp_file.filename = temp_file.filename.rpartition('/')[2] if temp_file.filename == file: current_file = temp_file; # Check whether graph exists yet. graph_exists = ds.check_exists(GRAPH_BUCKET + '/' + file.partition('.')[0] + '.png', None) # Get permissions for file. permissions = None if file_info is not None: user_key = ps.get_user_key_by_id(authed_user.user_id()) permissions = ps.get_user_file_permissions(file_info.key, user_key) template_dict = {'current_file' : current_file, 'name' : file, 'authed_user_nick': authed_user_nick, 'file_info' : file_info, 'graph_ready' : graph_exists, 'undo_link' : undo_uri, 'permissions' : permissions } ## Get gating information. info_path = INFO_BUCKET + '/' + file.partition('.')[0] + '.txt' if ds.check_exists(info_path, None): buffer = ds.open(info_path) if buffer: file_text = buffer.read() stats = file_text.split(' ') if len(stats)>=3: template_dict.update( { 'gating_stats' : { 'selection' : stats[0], 'total' : stats[1], 'percent' : float(stats[2])*100 } } ) ## Get different axis axis_path = INFO_BUCKET + '/' + file.partition('.')[0] + 'info.txt' if ds.check_exists(axis_path, None): buffer = ds.open(axis_path) if buffer: file_text = buffer.read() axis = file_text.split('\n') if len(axis)>0: while '' in axis: axis.remove('') template_dict.update( { 'available_axes' : axis } ) return render(request, 'file_preview.html', template_dict)
def test_addfile_checkCont(self): ds.add_file("/test_bucket/test_dir/test_file_3.ext", "testing-testing-testing", None) fh = ds.open("/test_bucket/test_dir/test_file_3.ext", 'r') self.assertEqual(fh.read(), "testing-testing-testing")