def rename(self, _id, path): record = self.find_by_id(_id) if not record: return False if record.get('is_dir'): records = self.find({ 'book_id': record['book_id'], 'parent': record.get('path') }) for doc in records: new_path = doc['path'].replace(doc['parent'], path) if doc.get('is_dir'): self.rename(doc['_id'], new_path) continue meta = get_meta(new_path) update = { 'path': new_path, 'parent': os.path.dirname(new_path), **meta, } self.update_one({'_id': doc['_id']}, {'$set': update}) meta = get_meta(path) update = { 'path': path, 'parent': os.path.dirname(path), **meta, } self.update_one({'_id': record['_id']}, {'$set': update}) return True
def load_from_dir(home_path, exclude=['*.retry'], links=False, book_name=None): bucket = [] cursor = 0 parent = home_path book_name = book_name or os.path.basename(home_path) pattern = '|'.join(exclude).replace('*', '.*?') for current, dirs, files in os.walk(home_path, topdown=True, followlinks=links): pathname = current.replace(home_path, '') or '/' if exclude: match = re.search(pattern, pathname) if match: continue dir_record = { 'book_name': book_name, 'path': pathname, 'is_dir': True, 'is_edit': False, 'seq_no': cursor, 'parent': None, 'created_at': int(time.time()), } if not current == home_path: dir_record['parent'] = parent meta = get_meta(pathname=pathname) dir_record.update(meta) parent = pathname bucket.append(dir_record) for file in files: pathname = parent.rstrip('/') + '/' + file if exclude: match = re.match(pattern, pathname) if match: continue cursor += 1 filename = current + '/' + file is_edit = Dumper.is_read(filename) file_record = dir_record.copy() file_record['is_edit'] = is_edit file_record['path'] = pathname file_record['parent'] = parent file_record['is_dir'] = False file_record['seq_no'] = cursor if is_edit: with open(filename, 'r', encoding='utf-8') as fd: file_record['content'] = fd.read() file_record['md5'] = md5(file_record['content']) file_record['is_encrypt'] = Vault.is_encrypted( file_record['content']) meta = get_meta(file_record['path']) file_record.update(meta) file_record['meta'] = meta bucket.append(file_record) cursor += 1 return bucket
def add_folder(): params = request.get_json() if not params or not params.get('id') or not params.get('folder'): return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 record_id = params.get('id') folder = params.get('folder') parent = params.get('parent') book_id = params.get('book_id') parent = parent if parent != '.' else '/' parent_path = None if parent != '/': parent_record = Playbook.find_one({ '_id': ObjectId(record_id), 'is_dir': True }) if not parent_record: return jsonify({ 'message': 'invalid params', 'code': 104001, }), 400 parent_path = parent_record.get('path') file_path = os.path.join(parent, folder) record = { 'path': file_path, 'book_id': book_id, 'parent': parent_path, 'is_dir': True, 'content': '', 'is_edit': False, 'add_by': login_user.get('username'), 'created_at': int(time.time()), 'updated_at': datetime.datetime.now().isoformat(), } meta = get_meta(file_path) record.update(meta) record['additions'] = meta check = Playbook.find_one({'book_id': book_id, 'path': record['path']}) if check: additions = check.get('additions') or {} additions.update(meta) record['additions'] = additions Playbook.update_one({'_id': check['_id']}, {'$set': record}) else: Playbook.insert_one(record) return jsonify({ 'message': 'ok', 'code': 0, })
def upload_playbook(_id): files = request.files record = Book.find_by_id(_id) if not record: return jsonify({ "message": "book not found", "code": 104004, }), 400 if not files: return jsonify({ 'message': 'invalid files params', 'code': 104001 }), 400 file = files['file'] filename = file.filename.lstrip('/') path_list = filename.split('/') filename = '/'.join(path_list[1:]) filename = '/' + filename home_path, basename = os.path.split(filename) file_list = set(_make_path(filename)) for dirname in file_list: check = Playbook.find_one({ 'book_id': _id, 'path': dirname, }) if not check: parent_path, name = os.path.split(dirname) parent_path = parent_path if parent_path != '/' else None parent = { 'path': dirname, 'is_dir': True, 'is_edit': False, 'book_id': _id, 'parent': parent_path, 'name': name, 'created_at': time.time(), } meta = get_meta(dirname) parent.update(meta) parent['additions'] = meta Playbook.insert_one(parent) data = { 'path': filename, 'is_dir': False, 'parent': home_path or None, 'book_id': _id } can_edit = is_edit(file) if not can_edit: file_id = db.save_file(filename=filename, fileobj=file) data['file_id'] = file_id else: content = file.stream.read() content = content.decode('utf-8') data['is_encrypt'] = Vault.is_encrypted(content) if data['is_encrypt']: # @todo vault password vault = Vault() data['content'] = vault.encrypt_string(content) data['md5'] = md5(content) else: data['content'] = content data['md5'] = md5(content) meta = get_meta(data['path']) data.update(meta) data['additions'] = meta data['is_edit'] = can_edit data['created_at'] = time.time() data['updated_at'] = time.time() Playbook.update_one({ 'path': filename, 'book_id': _id }, {'$set': data}, upsert=True) return jsonify({ "message": "ok", "code": 0, })
def upload(): files = request.files form = request.form if not form or not form.get('parent'): return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 parent_id = form.get('parent') book_id = form.get('bookId') if parent_id == '/' and book_id: book = db.collection('books').find_one({'_id': ObjectId(book_id)}) if not book: return jsonify({ "message": "record not found", "code": 104040, }), 404 parent = { 'path': '', 'book_id': book_id } else: parent = db.collection('playbook').find_one({'_id': ObjectId(parent_id)}) if not parent: return jsonify({ "message": "parent path not found", "code": 104004, }), 400 file = files['files'] filename = file.filename path = parent['path'] + '/' + filename record = { 'book_id': parent.get('book_id'), 'path': path, 'is_dir': False, } meta = get_meta(path) record.update(meta) can_edit = is_edit(file) if not can_edit: file_id = db.save_file(filename=filename, fileobj=file) record['file_id'] = file_id else: content = file.read() content = content.decode('utf-8') record['content'] = content record['is_edit'] = can_edit record['created_at'] = int(time.time()) record['updated_at'] = datetime.datetime.now().isoformat() # exist = db.collection('playbook').find_one({'path': path}) # if exist: # db.collection('playbook').update_one({}) # return jsonify({ # "message": "ok", # "code": 104005, # }), 400 db.collection('playbook').update_one({ 'path': path, 'book_id': ObjectId(parent['book_id']), }, { '$set': record, }, upsert=True) return jsonify({ "message": "ok", "code": 0, })
def upload(): files = request.files form = request.form if not form or not form.get('parent'): return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 if not files.get('files'): return jsonify({ 'message': 'illegal param', 'code': 104001, }), 400 parent_id = form.get('parent') book_id = form.get('bookId') if parent_id == '/' and book_id: book = Book.find_one({'_id': ObjectId(book_id)}) if not book: return jsonify({ "message": "record not found", "code": 104040, }), 404 current = {'path': '/', 'book_id': book_id} else: current = Playbook.find_one({'_id': ObjectId(parent_id)}) if not current: return jsonify({ "message": "current path not found", "code": 104004, }), 400 file = files['files'] filename = file.filename path = os.path.join(current['path'], filename) # parent = Playbook.find_one({'book_id': book_id, 'path': os.path.dirname(path)}) record = { 'book_id': book_id, 'path': path, 'is_dir': False, 'parent': os.path.dirname(path), } meta = get_meta(path) record.update(meta) can_edit = is_edit(file) if not can_edit: file_id = db.save_file(filename=filename, fileobj=file) record['file_id'] = file_id else: content = file.read() content = content.decode('utf-8') record['content'] = content record['is_edit'] = can_edit record['created_at'] = int(time.time()) record['updated_at'] = datetime.datetime.now().isoformat() # where = { # 'path': path, # 'book_id': ObjectId(book_id) # } # update = { # '$set': record, # } check_playbook_node(record) # Playbook.update_one(where, update=update, upsert=True) return jsonify({ 'message': 'ok', 'code': 0, })