rename files and folders
This commit is contained in:
		
							parent
							
								
									4779aeace9
								
							
						
					
					
						commit
						846799dacf
					
				
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
				
			
			@ -1,5 +1,48 @@
 | 
			
		|||
$(document).on('page:browse', function() {
 | 
			
		||||
  $("body").off('click', '.delete').on('click', '.delete', function(event) {
 | 
			
		||||
  $('body').off('click', '.rename').on('click', '.rename', function(event) {
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
    button = $(this);
 | 
			
		||||
 | 
			
		||||
    var filename = prompt("New file name:");
 | 
			
		||||
 | 
			
		||||
    if (filename == "") {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (filename.substring(0, 1) != "/") {
 | 
			
		||||
        filename = window.location.pathname.replace("/admin/browse/", "") + '/' + filename;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var content = '{"filename": "' + filename + '"}';
 | 
			
		||||
 | 
			
		||||
    $.ajax({
 | 
			
		||||
      type: 'PUT',
 | 
			
		||||
      url: button.data("file"),
 | 
			
		||||
      data: content,
 | 
			
		||||
      dataType: 'json',
 | 
			
		||||
      encode: true
 | 
			
		||||
    }).done(function(data) {
 | 
			
		||||
      $.pjax({
 | 
			
		||||
        url: window.location.pathname,
 | 
			
		||||
        container: '#content'
 | 
			
		||||
      });
 | 
			
		||||
      notification({
 | 
			
		||||
        text: button.data("message"),
 | 
			
		||||
        type: 'success',
 | 
			
		||||
        timeout: 5000
 | 
			
		||||
      });
 | 
			
		||||
    }).fail(function(data) {
 | 
			
		||||
      notification({
 | 
			
		||||
        text: 'Something went wrong.',
 | 
			
		||||
        type: 'error'
 | 
			
		||||
      });
 | 
			
		||||
      console.log(data);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  $('body').off('click', '.delete').on('click', '.delete', function(event) {
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
    button = $(this);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -24,7 +67,7 @@ $(document).on('page:browse', function() {
 | 
			
		|||
    return false;
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  $('.new').click(function(event) {
 | 
			
		||||
  $('.new').off('click').click(function(event) {
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
 | 
			
		||||
    if ($(this).data("opened")) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,8 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
// ServeHTTP is used to serve the content of Browse page
 | 
			
		||||
// using Browse middleware from Caddy
 | 
			
		||||
// using Browse middleware from Caddy. It handles the requests
 | 
			
		||||
// for DELETE, POST, GET and PUT related to /browse interface.
 | 
			
		||||
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
 | 
			
		||||
	// Removes the page main path from the URL
 | 
			
		||||
	r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +22,8 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
 | 
			
		|||
		return POST(w, r, c)
 | 
			
		||||
	case "GET":
 | 
			
		||||
		return GET(w, r, c)
 | 
			
		||||
	case "PUT":
 | 
			
		||||
		return PUT(w, r, c)
 | 
			
		||||
	default:
 | 
			
		||||
		return 400, errors.New("Invalid method.")
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
package browse
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/config"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// PUT handles the HTTP PUT request for all /admin/browse related requests.
 | 
			
		||||
// Renames a file and/or a folder.
 | 
			
		||||
func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
 | 
			
		||||
	// Remove both beginning and trailing slashes
 | 
			
		||||
	old := r.URL.Path
 | 
			
		||||
	old = strings.TrimPrefix(old, "/")
 | 
			
		||||
	old = strings.TrimSuffix(old, "/")
 | 
			
		||||
	old = c.Path + old
 | 
			
		||||
 | 
			
		||||
	// Get the JSON information sent using a buffer
 | 
			
		||||
	buffer := new(bytes.Buffer)
 | 
			
		||||
	buffer.ReadFrom(r.Body)
 | 
			
		||||
 | 
			
		||||
	// Creates the raw file "map" using the JSON
 | 
			
		||||
	var info map[string]interface{}
 | 
			
		||||
	json.Unmarshal(buffer.Bytes(), &info)
 | 
			
		||||
 | 
			
		||||
	// Check if filename and archetype are specified in
 | 
			
		||||
	// the request
 | 
			
		||||
	if _, ok := info["filename"]; !ok {
 | 
			
		||||
		return 400, errors.New("Filename not specified.")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Sanitize the file name path
 | 
			
		||||
	new := info["filename"].(string)
 | 
			
		||||
	new = strings.TrimPrefix(new, "/")
 | 
			
		||||
	new = strings.TrimSuffix(new, "/")
 | 
			
		||||
	new = c.Path + new
 | 
			
		||||
 | 
			
		||||
	err := os.Rename(old, new)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 500, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
	w.Write([]byte("{}"))
 | 
			
		||||
	return 200, nil
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue