Kangsubeen

add copy/move

......@@ -78,6 +78,10 @@ class FileCopyMove(APIView):
"""
Download or delete a file instance.
"""
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
#TODO is folder move, copy well?
# move
def post(self, request, old_path, new_path, format=None):
......
......@@ -16,52 +16,82 @@
</div>
</center>
<div>
<button class="ui primary button" style="margin-left: 28%" onclick="make_folder()"><i class="plus icon"></i>폴더 만들기</button>
<button class="ui primary button" style="margin-left: 24%" onclick="make_folder()"><i class="plus icon"></i>폴더 만들기</button>
<div style="display: none;" id="dir_make">
<input type="text" id="dir_name">
<button class="ui primary button" onclick="make_directory({{path}} )">만들기</button>
<button class="ui primary button" onclick="make_directory()">만들기</button>
</div>
</div>
<br>
<center>
<table style="margin-top: 7px" class="ui celled striped collapsing table">
<thead>
<tr><th class="twelve wide">/{{ path }}</th>
<th>복사</th>
<th >이동</th>
<th>삭제</th>
<th >다운로드</th>
<tr><th class="nine wide">/{{ path }}</th>
<th class="center aligned">경로</th>
<th class="center aligned">복사</th>
<th class="center aligned">이동</th>
<th class="collapsing">삭제</th>
<th class="collapsing">다운로드</th>
</tr></thead>
<tbody>
{% for file in files %}
{% if path != "" %}
<td>
<a onclick="go_parent()"><i class="folder icon"></i> ...</a>
</td>
{% endif %}
<tr>
{% for file in files %}
<td class="collapsing">
{% if file.type == "directory" %}
{% with new_path=path|add:file.name|add:'/' %}
<a href="{% url 'file_list' path=new_path %}"><i class="folder icon"></i> {{file.name}}</a>
<a href="{% url 'file_list' path=new_path %}" ><i class="folder icon"></i> {{file.name}}</a>
{% endwith %}
{% else %}
<i class="file outline icon"></i> {{file.name}}
{% with new_path=path|add:file.name %}
<a style="color: black" href="{% url 'file_view' path=new_path %}"><i class="file outline icon"></i> {{file.name}} </a>
{% endwith %}
{% endif %}
</td>
<td class="center aligned collapsing">
{% with new_path=path|add:file.name|add:'/' %}
<a href="#"><i class="copy outline icon"></i></a>
{% endwith %}
<td>
{% if file.type != "directory" %}
{% with old_path=path|add:file.name %}
<input placeholder="dir/test.txt" type="text" id="{{ old_path }}">
{% endwith %}
{% endif %}
</td>
<td class="center aligned collapsing"><i onclick="make_folder()" style="cursor: pointer;" class="sign out alternate icon"></i></td>
<td class="collapsing">
<div>
{% if file.type != "directory" %}
{% with old_path=path|add:file.name %}
<a style="color: black" id= "copy_path" onclick="copy('{{ old_path }}')"><i class="copy outline icon"></i></a>
{% endwith %}
{% endif %}
</div>
</td>
<td class="center aligned collapsing">
{% with new_path=path|add:file.name %}
<a href="{% url 'file_delete' path=new_path %}"><i class="trash alternate outline icon"></i></a>
{% endwith %}
</td>
<td class="center aligned collapsing">
{% with new_path=path|add:file.name %}
<a href="{% url 'file_download' path=new_path %}"><i style="cursor: pointer;" class="download icon"></i></a>
{% endwith %}
</td>
{% if file.type != "directory" %}
{% with old_path=path|add:file.name %}
<a style="color: black" onclick="move('{{ old_path }}')"><i class="sign out alternate icon"></i></a>
{% endwith %}
{% endif %}
</td>
<td class="center aligned collapsing">
{% with new_path=path|add:file.name %}
<a style="color: black" href="{% url 'file_delete' path=new_path %}"><i class="trash alternate outline icon"></i></a>
{% endwith %}
</td>
<td class="center aligned collapsing">
{% with new_path=path|add:file.name %}
<a style="color: black" href="{% url 'file_download' path=new_path %}"><i class="download icon"></i></a>
{% endwith %}
</td>
</tr>
{% endfor %}
</tbody>
......@@ -83,24 +113,33 @@
document.getElementById("dir_make").style.display = "inline-block";
}
function make_directory(dir_path){
function make_directory(){
dir = document.getElementById("dir_name").value;
console.log(dir)
var dir_path = "{{ path }}";
new_path = dir_path + dir + '/';
location.href = "{% url 'make_folder' path='' %}" + new_path;
}
function copy(){
function go_parent(){
var dir_path = "{{ path }}";
var dir_arr = dir_path.split('/');
dir_arr.pop();
dir_arr.pop();
var new_path = dir_arr.join('/');
location.href = "{% url 'file_list' path='' %}" + new_path;
}
function move(){
function copy(old){
var copy_path = document.getElementById(old).value;
location.href = "/copy/"+old+"&" + copy_path;
}
function delete_file(){
function move(old){
var move_path = document.getElementById(old).value;
location.href = "/move/"+old+"&" + move_path;
}
function download(){
}
</script>
{% endblock %}
\ No newline at end of file
......
......@@ -12,9 +12,10 @@ urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^list/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/)*)$', views.file_list, name='file_list'),
url(r'^upload/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/)*)$', views.file_upload, name='file_upload'),
url(r'^view/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)$', views.file_view, name='file_view'),
url(r'^make_folder/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/)*)$', views.make_folder, name='make_folder'),
url(r'^copy/(?P<old_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)&(?P<new_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]]*/*)*)$', views.file_copy, name='file_copy'),
url(r'^move/(?P<old_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)&(?P<new_path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]]*/*)*)$', views.file_move, name='file_move'),
url(r'^delete/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)$', views.file_delete, name='file_delete'),
url(r'^download/(?P<path>([\w\s가-힣.\`\'\˜\=\+\#\ˆ\@\$\&\-\.\(\)\{\}\;\[\]]*/*)*)$', views.file_download, name='file_download'),
]
\ No newline at end of file
......
......@@ -76,4 +76,29 @@ def file_view(request, path):
response = HttpResponse(fh.read(), content_type='text/plain' )
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
\ No newline at end of file
raise Http404
@login_required
def file_copy(request, old_path, new_path):
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
files = requests.post('http://localhost:8000/restapi/file-mod/'+old_path+'&'+new_path, data={'method': 'cp'}, headers=headers, cookies=cookies)
print(files.json())
new_path = "/".join(new_path.split("/")[:-1])
if new_path != '':
new_path = new_path+'/'
return redirect('file_list', path=new_path)
@login_required
def file_move(request, old_path, new_path):
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
files = requests.post('http://localhost:8000/restapi/file-mod/'+old_path+'&'+new_path, data={'method': 'mv'}, headers=headers, cookies=cookies)
print(files.json())
new_path = "/".join(new_path.split("/")[:-1])
if new_path != '':
new_path = new_path+'/'
return redirect('file_list', path=new_path)
\ No newline at end of file
......