From f4cc1aaf3c6cf62dc782b74269555a18707868be Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 16 Apr 2011 13:42:06 -0400 Subject: [PATCH] Start work on rewriting the TV renamer in Go --- build.py | 3 ++- src/paths.go | 12 +++++++++++- src/server.go | 13 +++++++++++++ src/tv_rename.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/tv_rename.go diff --git a/build.py b/build.py index 18b0346..df3fcef 100755 --- a/build.py +++ b/build.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2.5 # # Armadillo File Manager -# Copyright (c) 2010, Robert Sesek +# Copyright (c) 2010-2011, Robert Sesek # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software @@ -32,6 +32,7 @@ VERSION_FILE = os.path.join(FE_PATH, 'version.js.proto') SOURCES = [ 'config.go', 'paths.go', + 'tv_rename.go', 'server.go', 'main.go' ] diff --git a/src/paths.go b/src/paths.go index a538116..27b92c1 100644 --- a/src/paths.go +++ b/src/paths.go @@ -1,6 +1,6 @@ // // Armadillo File Manager -// Copyright (c) 2010, Robert Sesek +// Copyright (c) 2010-2011, Robert Sesek // // This program is free software: you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free Software @@ -41,6 +41,16 @@ func checkInJail(the_path string) bool { return true } +// Verifies that the path is in the jail and returns the cleaned-up path. Will +// return nil on error. +func Verify(thePath string) *string { + fullPath := canonicalizePath(thePath) + if !checkInJail(fullPath) { + return nil + } + return &fullPath +} + func List(the_path string) (files vector.StringVector, err os.Error) { full_path := canonicalizePath(the_path) if !checkInJail(full_path) { diff --git a/src/server.go b/src/server.go index 07674a0..72ade34 100644 --- a/src/server.go +++ b/src/server.go @@ -20,6 +20,7 @@ import ( "strings" "./config" "./paths" + "./tv_rename" ) var dir, file = path.Split(path.Clean(os.Getenv("_"))) @@ -71,7 +72,19 @@ func serviceHandler(response http.ResponseWriter, request *http.Request) { } okResponse(response, data) } + case "tv_rename": + newPath, err := tv_rename.RenameEpisode(request.FormValue("path")) + if err != nil { + errorResponse(response, err.String()) + } else { + data := map[string] interface{} { + "path" : *newPath, + "error" : 0, + } + okResponse(response, data) + } default: + fmt.Printf("Invalid action: '%s'\n", request.FormValue("action")) errorResponse(response, "Unhandled action") } } diff --git a/src/tv_rename.go b/src/tv_rename.go new file mode 100644 index 0000000..c27de00 --- /dev/null +++ b/src/tv_rename.go @@ -0,0 +1,36 @@ +// +// Armadillo File Manager +// Copyright (c) 2011, Robert Sesek +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or any later version. +// + +package tv_rename + +import ( + "fmt" + "os" + "./paths" +) + +// Takes a full file path and renames the last path component as if it were a +// TV episode. This performs the actual rename as well. +func RenameEpisode(inPath string) (*string, os.Error) { + // Make sure a path was given. + if len(inPath) < 1 { + return nil, os.NewError("Invalid path") + } + // Check that it's inside the jail. + var path *string = paths.Verify(inPath) + if path == nil { + return nil, os.NewError("Path is invalid or outside of jail") + } + // Make sure that the file exists. + fileInfo, err := os.Stat(*path) + if err != nil { + return nil, err + } + return path, nil +} -- 2.43.5