diff --git a/src/jTransfer/RenameFileServlet.java b/src/jTransfer/RenameFileServlet.java new file mode 100644 index 0000000..f062f3a --- /dev/null +++ b/src/jTransfer/RenameFileServlet.java @@ -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"); + } + } +} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 06d4285..a8c82df 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -114,6 +114,15 @@ /rm + + RenameFileServlet + jTransfer.RenameFileServlet + + + RenameFileServlet + /rename + + jTransfer.TransferSessionListener diff --git a/web/js/interface.js b/web/js/interface.js index 5b5a979..2cefe19 100644 --- a/web/js/interface.js +++ b/web/js/interface.js @@ -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 = $('
', {id: 'lightbox'}); var content = $('
', {class: 'container'}); var jumbo = $('
', {class: 'jumbotron'}); @@ -26,9 +26,10 @@ $(document).ready(function () { var changeDiv = $('
').appendTo(jumbo); var deleteLink = $('Delete'); - var renameLink = $('Rename'); + var renameLink = $('Rename'); deleteLink.click(rmClick); + renameLink.click(renameClick); deleteLink.appendTo(changeDiv); $(' | ').appendTo(changeDiv); @@ -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; @@ -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'); @@ -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)) { @@ -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); });