ref: bff1f1e6893a365056afd8abb47b81c592c12d55
parent: ef2ad4d91fa41b364f21d290a12e977cc5cf676f
author: spf13 <[email protected]>
date: Thu May 1 21:04:14 EDT 2014
Adding some new methods to helpers (GuessSection, MakeTitle & Filename)
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -34,6 +34,10 @@
return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
}
+func MakeTitle(inpath string) string {
+ return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1)
+}
+
func Sanitize(s string) string {
return sanitizeRegexp.ReplaceAllString(s, "")
}
@@ -88,6 +92,11 @@
return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath))
}
+func Filename(in string) (name string) {
+ name, _ = FileAndExt(in)
+ return
+}
+
func FileAndExt(in string) (name string, ext string) {
ext = path.Ext(in)
base := path.Base(in)
@@ -101,6 +110,18 @@
return
}
+func GuessSection(in string) string {
+ x := strings.Split(in, "/")
+ x = x[:len(x)-1]
+ if len(x) == 0 {
+ return ""
+ }
+ if x[0] == "content" {
+ x = x[1:]
+ }
+ return path.Join(x...)
+}
+
func PathPrep(ugly bool, in string) string {
if ugly {
return Uglify(in)
@@ -152,6 +173,35 @@
}
return path, nil
+}
+
+func SafeWriteToDisk(inpath string, r io.Reader) (err error) {
+ dir, _ := filepath.Split(inpath)
+ ospath := filepath.FromSlash(dir)
+
+ if ospath != "" {
+ err = os.MkdirAll(ospath, 0777) // rwx, rw, r
+ if err != nil {
+ return
+ }
+ }
+
+ exists, err := Exists(inpath)
+ if err != nil {
+ return
+ }
+ if exists {
+ return fmt.Errorf("%v already exists", inpath)
+ }
+
+ file, err := os.Create(inpath)
+ if err != nil {
+ return
+ }
+ defer file.Close()
+
+ _, err = io.Copy(file, r)
+ return
}
func WriteToDisk(inpath string, r io.Reader) (err error) {