def get_details(token_dir): path, request_path = prepare_path(token_dir, request.args.get('path', '')) request_filename = request.args.get('filename') if not request_filename: return {'message': 'File name not provided'}, 400 path = f'{path}{request_filename}' if os.path.exists(path): file_details = {'size': size(os.path.getsize(path)), 'created': datetime.fromtimestamp(os.path.getctime(path))} return {'path': request_path, 'filename': request_filename, 'details': file_details}, 200 return {'message': 'File not found'}, 404
def delete_file(token_dir): path, request_path = prepare_path(token_dir, request.json.get('path', '')) request_filename = request.json.get('filename') if not request_filename: return {'message': 'File name not provided'}, 400 path = f'{path}{request_filename}' if os.path.exists(path): os.remove(path) else: return {'message': 'File not found'}, 404 return {'message': 'OK'}, 200
def get_dir(token_dir): path, request_path = prepare_path(token_dir, request.args.get('path', '')) try: dir_list = [{ 'name': entry.name, 'is_file': entry.is_file(), 'size': size(entry.stat().st_size), 'created': datetime.fromtimestamp(entry.stat().st_ctime) } for entry in os.scandir(path=path)] except (FileNotFoundError, NotADirectoryError): return {'message': 'Directory not found'}, 404 return {'path': request_path, 'dir': dir_list}, 200
def create(token_dir): path, request_path = prepare_path(token_dir, request.json.get('path', '')) request_filename = request.json.get('filename') if not request_filename: return {'message': 'File name not provided'}, 400 content = '' if not os.path.exists(path): os.makedirs(path) # if os.path.exists(path + request_filename): # return {'message': 'File already exists'}, 400 with open(os.path.join(path, request_filename), 'w') as temp_file: temp_file.write(content) return {'message': 'Created'}, 201
def delete_dir(token_dir): path, request_path = prepare_path(token_dir, request.json.get('path', '')) request_dirname = request.json.get('dirname') if not request_dirname: return {'message': 'Directory name not provided'}, 400 path = f'{path}{request_dirname}' if os.path.exists(path): try: os.rmdir(path) except OSError as e: if e.errno in (39, 66): return {'message': 'Directory not empty'}, 400 else: raise else: return {'message': 'Directory not found'}, 404 return {'message': 'OK'}, 200