def move(): global DATA_DIR req = request.get_data().decode('utf-8') req = json.loads(req) logger.info('data move: {}'.format(req)) src = safepath(req.pop('src_path')) dest = safepath(req.pop('dest_path')) src = os.path.join(DATA_DIR, src) dest = os.path.join(DATA_DIR, dest) os.rename(src, dest) logger.info('data move: succeed {} {}'.format(src, dest))
def createset(): global DATA_DIR req = request.get_data().decode('utf-8') req = json.loads(req) logger.info('data createset: {}'.format(req)) path = req.pop('dataset') path = safepath(path) path = os.path.join(DATA_DIR, path) os.mkdir(path) logger.info('data createset: succeed {}'.format(path))
def get(): global DATA_DIR req = request.get_data().decode('utf-8') req = json.loads(req) logger.info('data get: {}'.format(req)) path = safepath(req.pop('dataset')) name = safepath(req.pop('name')) path = os.path.join(DATA_DIR, path, name) if not os.path.exists(path): ret = { 'succeed': 0, 'message': "File Not Exists", } elif not os.path.isfile(path): ret = { 'succeed': 0, 'message': path + ' is not a file', } else: ret = sample_data(path, type_='path') logger.info('data get: ret {}'.format(ret)) return ret
def download(path): global DATA_DIR logger.info('data download: {}'.format(path)) path = safepath(path) path = os.path.join(os.getcwd(), DATA_DIR, path) print(path) def send_chunk(filepath): with open(filepath, 'rb') as f: while True: chunk = f.read(20 * 1024 * 1024) if not chunk: break yield chunk return Response(send_chunk(path), content_type='application/octet-stream')
def upload(): # TODO : return message global DATA_DIR path = request.form.get('dataset') logger.info('data upload: path {}'.format(path)) path = safepath(path) file = request.files['file'] path = os.path.join(DATA_DIR, path) file.save(path) size = os.path.getsize(path) ftype = get_type(path) logger.info('data upload: succeed, type: {} size: {}'.format(ftype, size)) return { "size": size, 'type': ftype, }
def delete(): global DATA_DIR req = request.get_data().decode('utf-8') req = json.loads(req) logger.info('data delete: {}'.format(req)) path = safepath(req.pop('path')) path = os.path.join(DATA_DIR, path) if os.path.isdir(path): for root, dirs, files in os.walk(path, topdown=False): for file in files: os.remove(os.path.join(root, file)) for dir in dirs: os.rmdir(os.path.join(root, dir)) os.rmdir(path) logger.info('data delete: succeed rm dir {}'.format(path)) else: os.remove(path) logger.info('data delete: succeed rm file {}'.format(path))
def datapath(path): global DATA_DIR path = safepath(path) path = os.path.join(DATA_DIR, path) return path