Skip to content

Commit

Permalink
Rename item. Fix #1427.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiarn committed Jan 31, 2023
1 parent db673db commit e1aece9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
17 changes: 17 additions & 0 deletions plugins/filemanager/resources/js/controllers/index.controller.es
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ angular.module('ajenti.filemanager').controller('FileManagerIndexController', fu
$scope.hideClipboard();
};

$scope.rename = function(item) {
messagebox.prompt(gettext('New name')).then((msg) => {
if (!msg.value) {
return;
}
dst = `${$scope.path}/${msg.value}`;
filesystem.rename(item.path, dst).then((resp) => {
if (!resp.data) {
notify.error(gettext("File or directory already exists"));
return
}
item.name = msg.value;
item.path = dst;
});
});
};

$scope.doCut = function() {
for (let item of $scope.items) {
if (item.selected) {
Expand Down
3 changes: 3 additions & 0 deletions plugins/filemanager/resources/partial/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<a ng:href="{{urlPrefix}}/view/filemanager/properties/{{item.path}}" class="list-group-btn subtle" title="Properties">
<i class="fa fa-cog"></i>
</a>
<a ng:click="rename(item)" class="list-group-btn subtle" title="Rename">
<i class="fa fa-pen"></i>
</a>
<div class="list-group-addon pull-left">
<span class="subtle" checkbox ng:model="item.selected"></span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ angular.module('ajenti.filesystem').service('filesystem', function($rootScope, $
this.createDirectory = (path) =>
$http.post(`/api/filesystem/create-directory/${path}`)

this.rename = (path, dst) =>
$http.post(`/api/filesystem/rename/${path}`, {'dst': dst})

this.downloadBlob = (content, mime, name) =>
setTimeout(() => {
let blob = new Blob([content], {type: mime});
Expand Down
28 changes: 27 additions & 1 deletion plugins/filesystem/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def handle_api_fs_read(self, http_context, path=None):
http_context.respond_not_found()
return 'File not found'
try:
content = open(path, 'rb').read()
with open(path, 'rb') as f:
content = f.read()
if http_context.query:
encoding = http_context.query.get('encoding', None)
if encoding:
Expand All @@ -65,6 +66,31 @@ def handle_api_fs_read(self, http_context, path=None):
http_context.respond_server_error()
return json.dumps({'error': str(e)})

@post(r'/api/filesystem/rename/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
def handle_api_fs_rename(self, http_context, path=None):
"""
Rename a file or a directory without overwriting
:param http_context: HttpContext
:type http_context: HttpContext
:param path: Path of the file
:type path: string
"""

if not os.path.isfile(path) and not os.path.isdir(path):
raise EndpointError

try:
dst = http_context.json_body()['dst']
if not os.path.isfile(dst) and not os.path.isdir(dst):
os.rename(path, dst)
return True
return False
except OSError as e:
raise EndpointError(e)

@post(r'/api/filesystem/write/(?P<path>.+)')
@authorize('filesystem:write')
@endpoint(api=True)
Expand Down

0 comments on commit e1aece9

Please sign in to comment.