def download_collection(collection, exclude_configs=False): directory = collection real_dir = real_directory(directory) dir_name = basename(dirname(real_dir)) fname = '%s.%s' % (dir_name, 'tar.gz') tmp_file_path = None try: _, tmp_file_path = mkstemp() tar_cmd_split = ['tar', '--exclude=.stats_cache'] if exclude_configs: tar_cmd_split.extend(['--exclude=annotation.conf', '--exclude=visual.conf', '--exclude=tools.conf', '--exclude=kb_shortcuts.conf']) tar_cmd_split.extend(['-c', '-z', '-f', tmp_file_path, dir_name]) tar_p = Popen(tar_cmd_split, cwd=path_join(real_dir, '..')) tar_p.wait() hdrs = [('Content-Type', 'application/octet-stream'), #'application/x-tgz'), ('Content-Disposition', 'inline; filename=%s' % fname)] if allowed_to_read(fpath): with open(tmp_file_path, 'rb') as tmp_file: tar_data = tmp_file.read() else: tar_data = "Access Denied" raise NoPrintJSONError(hdrs, tar_data) finally: if tmp_file_path is not None: remove(tmp_file_path)
def download_file(document, collection, extension): directory = collection real_dir = real_directory(directory) fname = '%s.%s' % (document, extension) fpath = path_join(real_dir, fname) #hdrs = [('Content-Type', 'text/plain; charset=utf-8'), ('Content-Disposition', 'inline; filename=%s' % fname)] hdrs = [('Content-Type', 'application/octet-stream'), ('Content-Disposition', 'inline; filename=%s' % fname)] if allowed_to_read(fpath): if not exists(fpath): data = "" if extension == "zip": import zipfile zipf = zipfile.ZipFile(fpath, 'w') zipf.close() with open(fpath, 'rb') as txt_file: data = txt_file.read() else: if extension != "zip": with open_textfile(fpath, 'r') as txt_file: data = txt_file.read().encode('utf-8') else: with open(fpath, 'rb') as txt_file: data = txt_file.read() else: data = "Access Denied" raise NoPrintJSONError(hdrs, data)
def _listdir(directory): # return listdir(directory) try: assert_allowed_to_read(directory) return [f for f in listdir(directory) if not _is_hidden(f) and allowed_to_read(path_join(directory, f))] except OSError, e: Messager.error("Error listing %s: %s" % (directory, e)) raise AnnotationCollectionNotFoundError(directory)
def _listdir(directory): # return listdir(directory) try: assert_allowed_to_read(directory) return [f for f in listdir(directory) if not _is_hidden(f) and allowed_to_read(path_join(directory, f))] except OSError as e: Messager.error("Error listing %s: %s" % (directory, e)) raise AnnotationCollectionNotFoundError(directory)
def _listdir(directory): # return listdir(directory) try: # 文件目录控制 assert_allowed_to_read(directory) return [ f for f in listdir(directory) if not _is_hidden(f) and allowed_to_read(path_join(directory, f)) ] except OSError as e: print('Error listing ' + directory + ': ' + e, file=sys.stderr)
def export_document(document, collection, extension): directory = collection real_dir = real_directory(directory) fname = '%s.%s' % (document, 'txt') fpath = path_join(real_dir, fname) rr = None if allowed_to_read(fpath): rr = ReadProject() owlfile, ttlfile = rr.read_project(real_dir, document, extension) fpaths = owlfile if extension[0:3] == 'owl' else ttlfile if extension[-1] == 's': fname = '%s.%s' % (document, "zip") hdrs = [('Content-Type', 'application/octet-stream'), ('Content-Disposition', 'inline; filename=%s' % fname)] from zipfile import ZipFile with ZipFile(path_join(real_dir, document) + ".zip", "w") as outfile: for f in fpaths: with open(f) as infile: outfile.writestr(f.split('/')[-1], infile.read()) with open(path_join(real_dir, document) + ".zip", 'rb') as tmp_file: data = tmp_file.read() else: fname = '%s.%s' % (document, extension) #hdrs = [('Content-Type', 'text/plain; charset=utf-8'), ('Content-Disposition', 'inline; filename=%s' % fname)] hdrs = [('Content-Type', 'application/octet-stream'), ('Content-Disposition', 'inline; filename=%s' % fname)] with open_textfile(fpaths, 'r') as txt_file: data = txt_file.read().encode('utf-8') else: data = "Access Denied" try: raise NoPrintJSONError(hdrs, data) finally: if rr: rr.clean_up() if isfile(path_join(real_dir, '%s.%s' % (document, 'zip'))): os.remove(path_join(real_dir, '%s.%s' % (document, 'zip')))
def assert_allowed_to_read(doc_path): if not allowed_to_read(doc_path): raise AccessDeniedError # Permission denied by access control