Skip to content

Commit

Permalink
added rename functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jack775544 committed May 25, 2016
1 parent e12b856 commit c7397fd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
50 changes: 50 additions & 0 deletions src/jTransfer/RenameFileServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package jTransfer;

import com.jcraft.jsch.ChannelSftp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

/**
* Created by jack775544 on 25/5/2016.
*/
@WebServlet(name = "RenameFileServlet")
public class RenameFileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Connection connection;

if (session.getAttribute(Connection.CONNECTION_NAME) instanceof Connection) {
connection = (Connection) session.getAttribute(Connection.CONNECTION_NAME);

// Set headers
String oldPath = request.getParameter("filename");
String newPath = request.getParameter("newname");
oldPath = URLDecoder.decode(oldPath, "UTF-8");
newPath = URLDecoder.decode(newPath, "UTF-8");

try {
// Get the SFTP Channel
ChannelSftp sftpChannel = connection.getSftpChannel();

sftpChannel.rename(oldPath, newPath);
} catch (Exception e) {
// Should never happen, I hope
MySqlLogger.logGeneral(e.getMessage(), session.getId());
}
} else {
// Bad connection, throw 500 error
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Connection is not valid");
}
}
}
9 changes: 9 additions & 0 deletions web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
<url-pattern>/rm</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>RenameFileServlet</servlet-name>
<servlet-class>jTransfer.RenameFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RenameFileServlet</servlet-name>
<url-pattern>/rename</url-pattern>
</servlet-mapping>

<listener>
<listener-class>jTransfer.TransferSessionListener</listener-class>
</listener>
Expand Down
24 changes: 18 additions & 6 deletions web/js/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $(document).ready(function () {
var ajaxConnect;
var pwd;

function buildLightbox(name, url, created, modified, size, linkname, rmUrl) {
function buildLightbox(name, url, created, modified, size, linkname, rmUrl, path) {
var lightbox = $('<div></div>', {id: 'lightbox'});
var content = $('<div></div>', {class: 'container'});
var jumbo = $('<div></div>', {class: 'jumbotron'});
Expand All @@ -26,9 +26,10 @@ $(document).ready(function () {

var changeDiv = $('<div></div>').appendTo(jumbo);
var deleteLink = $('<a href="' + rmUrl + '" id="delete">Delete</a>');
var renameLink = $('<a href="' + editUrl + '" target="_blank">Rename</a>');
var renameLink = $('<a href="#" target="_blank" data-path="' + path + '">Rename</a>');

deleteLink.click(rmClick);
renameLink.click(renameClick);

deleteLink.appendTo(changeDiv);
$('<span> | </span>').appendTo(changeDiv);
Expand Down Expand Up @@ -56,7 +57,6 @@ $(document).ready(function () {

function rmClick(e){
e.preventDefault();
console.log(this.href);
var confirmation = confirm("Delete this file?");
if (confirmation != true) {
return;
Expand All @@ -66,6 +66,20 @@ $(document).ready(function () {
ajaxConnect();
})
}

function renameClick(e){
e.preventDefault();
var newName = prompt("Enter the new file name");
var altPwd = pwd;
if (altPwd.slice(-1) != '/'){
altPwd = altPwd + '/';
}
newName = altPwd + newName;
$.get(common.buildUrl('./rename', {filename: this.dataset.path, newname: newName}), function(){
$("#lightbox").remove();
ajaxConnect();
});
}

ajaxConnect = function(){
var itemList = $('#items');
Expand Down Expand Up @@ -112,8 +126,6 @@ $(document).ready(function () {
var img = 'img';
var path = pwd + "/" + filename;
//var url = 'get?filename=' + path;
console.log(path);
console.log(filename);
var url = common.buildUrl('./get', {filename: path, name: filename});
var rmUrl = common.buildUrl('./rm', {filename: path, name: filename});
switch (Number(type)) {
Expand Down Expand Up @@ -141,7 +153,7 @@ $(document).ready(function () {

$('.file').click(function (e) {
e.preventDefault();
var lightbox = buildLightbox(this.dataset.name, this.href, this.dataset.created, this.dataset.modified, this.dataset.size, this.dataset.linkname, this.dataset.rmlink);
var lightbox = buildLightbox(this.dataset.name, this.href, this.dataset.created, this.dataset.modified, this.dataset.size, this.dataset.linkname, this.dataset.rmlink, this.dataset.path);
$("body").append(lightbox);
});

Expand Down

0 comments on commit c7397fd

Please sign in to comment.