def _save(self, path=None): try: data = request.form['data'] # Newline fix (remove cr) data = data.replace('\r', '').rstrip() fullpath = git_absolute_path(path) os.makedirs(os.path.dirname(fullpath), exist_ok=True) with open(fullpath, 'w') as f: f.write(data) f.write('\n') return jsonify({'path': normalize_path(path)}) except Exception as ex: logging.error(ex) if hasattr(ex, 'strerror'): message = ex.strerror elif hasattr(ex, 'message'): message = ex.message else: message = str(ex) return jsonify({ 'path': normalize_path(path), 'error': { 'message': 'Error saving {path}: {message}'.format(path=path, message=message) }, })
def get_files_node(path: Path, args: Args) -> TreeOutput: "Get tree files node" def try_listdir(path: str) -> List[str]: try: return os.listdir(dirpath) except IOError: return [] result = [] dirpath: str = git_absolute_path(path) long_ = 'long' in args for name in sorted(try_listdir(dirpath)): if name.startswith('.') or name == '__pycache__': continue fullname = os.path.join(dirpath, name) s = os.stat(fullname) leaf = not stat.S_ISDIR(s.st_mode) if long_: # Long format size = s.st_size if leaf else len(try_listdir(fullname)) result.append({ 'id': name, 'leaf': leaf, 'size': size, 'mode': s.st_mode, 'mtime': datetime.fromtimestamp(int(s.st_mtime)).isoformat() }) else: # Short format result.append({'id': name, 'leaf': leaf}) return result
def _load(self, path): try: code = None fullpath = git_absolute_path(path) # Read code with open(fullpath, 'r') as f: code = f.read().rstrip('\n') except Exception as ex: logging.error(ex) flash('Error loading file [{path}]'.format(path=path), 'error') finally: return code
def get_files_node(path: Optional[str] = None) -> List[Dict[str, Any]]: " Get tree files node " result = [] dirpath: str = git_absolute_path(path) for name in sorted(os.listdir(dirpath)): if name.startswith('.') or name == '__pycache__': continue fullname = os.path.join(dirpath, name) leaf = not os.path.isdir(fullname) result.append({ 'id': name, 'leaf': leaf, 'icon': 'fa-file' if leaf else 'fa-folder' }) return result
def _save(self, path): try: code = None code = request.form['code'] # Newline fix (remove cr) code = code.replace('\r', '').rstrip() fullpath = git_absolute_path(path) with open(fullpath, 'w') as f: f.write(code) f.write('\n') except Exception as ex: logging.error(ex) flash('Error saving file [{path}]'.format(path=path), 'error') finally: return code
def _save(self, path=None): try: data = request.form["data"] # Newline fix (remove cr) data = data.replace("\r", "").rstrip() fullpath = git_absolute_path(path) os.makedirs(os.path.dirname(fullpath), exist_ok=True) with open(fullpath, "w") as f: f.write(data) f.write("\n") return prepare_api_response(path=normalize_path(path)) except Exception as ex: logging.error(ex) return prepare_api_response( path=normalize_path(path), error_message="Error saving {path}: {message}".format( path=path, message=error_message(ex)), )
def _download(self, session, path): " Send the contents of a file to the client " try: if path.startswith('~git/'): # Download git blob - path = '~git/<hash>/<name>' _, path, filename = path.split('/', 3) response = execute_git_command(["cat-file", "-p", path]) response.headers[ 'Content-Disposition'] = 'attachment; filename="%s"' % filename try: content_type = mimetypes.guess_type(filename)[0] if content_type: response.headers['Content-Type'] = content_type except Exception: pass return response else: # Download file fullpath = git_absolute_path(path) return send_file(fullpath, as_attachment=True) except Exception as ex: logging.error(ex) abort(HTTP_404_NOT_FOUND)