def edit(request, repo_name, branch=REPO_BRANCH, path=None): file_source = "" msgs = [] json_convert = None if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, result_msg) if path[-1:] == "/": path = path[:-1] file_path = path #!!! FIX security repo = get_repo(repo_name) tree = repo.tree() # edit only if exist in tree try: tree = tree[path] except KeyError: msg.append(MSG_NO_FILE_IN_TREE) return error_view(request, msg) # edit only if it is file if not tree.type is "blob": msgs.append(MSG_CANT_VIEW) return error_view(request, msg) mime = tree.mime_type.split("/") file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, repo=repo, mime=tree.mime_type, size=tree.size, tree=tree, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) if file_meta["mime_type"] in EDITABLE_MIME_TYPES: form_class = TextFileEditForm else: form_class = FileEditForm signals.file_edit_start.send(sender=repo, file_path=file_path, url="") if request.method == "POST": form = form_class(request.POST, request.FILES) if form.is_valid(): file_abs_path = os.path.join(repo.working_dir, file_path) if file_meta["mime_type"] == "text" or mime[1] in ["xml"]: file_source = form.cleaned_data["file_source"] file_writen = write_file(file_abs_path, file_source) else: file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"]) if file_writen: msgs.append("File has been saved") message = form.cleaned_data["message"] ammend = form.cleaned_data.get("ammend", None) msg = mk_commit(repo, message, file_path, ammend) msgs.append(msg) else: msgs.append(MSG_CANT_SAVE_FILE) else: msgs.append(form.errors) if request.is_ajax(): json_convert = message_convert else: if file_meta["mime_type"] in EDITABLE_MIME_TYPES: file_source = tree.data_stream[3].read else: file_source = file_meta["abspath"] form = form_class(initial={"file_source": file_source, "message": "modified %s" % path}) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, form=form, file_source=file_source, breadcrumbs=make_crumbs(path), file_meta=file_meta, msg=msgs, repo_name=repo_name, branch_name=branch, repo=repo, delete_form=FileDeleteForm(initial={"message": MSG_DELETE_SUCCESS % path}), path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/edit.html", context, json_convert)
def view(request, repo_name, branch, path, commit_sha=None): """ view file in the commit """ file_source = diff = "" if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view(request, msg) file_path = path #!!! FIX security if path[-1:] == "/": path = path[:-1] repo = get_repo(repo_name) commit, tree = get_commit_tree(repo, commit_sha) if commit.parents: diff = get_diff(repo, path, commit.parents[0].hexsha, commit.hexsha) try: tree = tree[path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) if not tree.type is "blob": msg = MSG_NO_FILE_IN_TREE return error_view(request, msg) mime = tree.mime_type.split("/") file_source = tree.data_stream[3].read() # import ipdb; ipdb.set_trace() file_meta = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, abspath=tree.abspath, mime=tree.mime_type, size=tree.size, tree=tree, path=tree.abspath, mime_type=mime[0], type=file_type_from_mime(tree.mime_type), ) context = dict( STRATUS_MEDIA_URL=STRATUS_MEDIA_URL, file_source=file_source, breadcrumbs=make_crumbs(path), commit=commit, diff=diff, file_meta=file_meta, repo_name=repo_name, repo=repo, branch_name=branch, path=path, name=path.split("/")[-1:][0], ) if mime[0] == "image": import base64 context["img_base"] = base64.b64encode(file_source) from getimageinfo import getImageInfo context["img_meta"] = getImageInfo(file_source) return mix_response(request, "stratus/view_file.html", context)
def edit(request, repo_name, branch=REPO_BRANCH, path=None ): result_msg = file_source = "" if path in FILE_BLACK_LIST: msg = MSG_NOT_ALLOWED return error_view( request, result_msg) if path[-1:] == "/": path = path[:-1] file_path = path #!!! FIX security repo = get_repo( repo_name ) tree = repo.tree() try: tree = tree[path] except KeyError: msg = MSG_NO_FILE_IN_TREE return error_view( request, msg) if not tree.type is "blob": msg = MSG_CANT_VIEW return error_view( request, msg) mime = tree.mime_type.split("/") file_meta = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, abspath = tree.abspath, mime = tree.mime_type, size = tree.size, tree = tree, mime_type = mime[0], type = file_type_from_mime(tree.mime_type), ) if file_meta["mime_type"] in EDITABLE_MIME_TYPES: form_class = TextFileEditForm else: form_class = FileEditForm if request.method == 'POST': form = form_class( request.POST, request.FILES ) if form.is_valid(): if file_meta["mime_type"] == "text": file_source = form.cleaned_data["file_source"] write_file(file_path, file_source ) else: handle_uploaded_file(file_path, request.FILES['file_source']) message = form.cleaned_data["message"] result_msg = mk_commit(repo, message, file_path ) else: result_msg = MSG_COMMIT_ERROR else: if file_meta["mime_type"] in EDITABLE_MIME_TYPES: file_source = tree.data_stream[3].read else: file_source = file_meta["abspath"] form = form_class( initial={ "file_source":file_source, "message":"modified %s" % path } ) context = dict( GITTER_MEDIA_URL = GITTER_MEDIA_URL, form= form, file_source = file_source, breadcrumbs = make_crumbs(path), file_meta = file_meta, result_msg = result_msg, repo_name = repo_name, branch_name = branch, delete_form = FileDeleteForm( initial={"message":MSG_DELETE % path }), path = path, ) return mix_response( request, 'commitlog/edit.html', context)