This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
114 lines (93 loc) · 3.23 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// SHIM - A web front end for the Hugo site generator
// Copyright (C) 2016 Cameron Conn
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
const (
embedFmt = "{{< figure src=\"files/%s\" title=\"Put Figure Name Here\" >}}"
)
// StaticFiles retrieves and returns a list of all of the statically-uploaded
// user files for this site.
func (s *Site) StaticFiles() []string {
staticPath := filepath.Join(s.Location, "static", "files")
if _, err := os.Stat(staticPath); os.IsNotExist(err) {
os.Mkdir(staticPath, 0755)
}
staticFiles := []string{}
staticLoc := filepath.Join(s.Location, "static", "files")
scanFunc := func(path string, fileInfo os.FileInfo, _ error) error {
if !fileInfo.IsDir() {
path, err := filepath.Rel(staticLoc, path)
if err != nil {
return err
}
staticFiles = append(staticFiles, path)
}
return nil
}
err := filepath.Walk(staticPath, scanFunc)
if err != nil {
log.Printf("Could not load static files: %s\n", err.Error())
}
return staticFiles
}
// AddStaticFile saves a `file` to the `path` in this site's static directory.
// If the file already exists, then return an error.
func (s *Site) AddStaticFile(path string, uploadFile io.Reader) error {
if _, err := s.GetStaticFile(path); err == nil {
return fmt.Errorf("Sorry, but that file already exists.")
}
// TODO: This is dangerous. Maybe make sure that path is a descendant of
// the static file path?
newFilePath := filepath.Join(s.Location, "static", "files", path)
fmt.Printf("New file path: %s\n", newFilePath)
newFile, err := os.Create(newFilePath)
defer newFile.Close()
if err != nil {
return err
}
_, err = io.Copy(newFile, uploadFile)
return err
}
// GetStaticFile A safe method for getting a static file according to its
// relative path to the site's static file directory.
func (s *Site) GetStaticFile(path string) (http.File, error) {
filesRoot := filepath.Join(s.Location, "static", "files")
staticFSRoot := http.Dir(filesRoot)
return staticFSRoot.Open(path)
}
// RemoveStaticFile A safe method for removing static files from a site's
// static file directory using the relative path
func (s *Site) RemoveStaticFile(path string) error {
_, err := s.GetStaticFile(path)
if err != nil {
return err
}
absPath, err := filepath.Abs(filepath.Join(s.Location, "static", "files", path))
if err == nil {
err = os.Remove(absPath)
}
return err
}
// GetEmbedCode Generate the embedding code for a static file used in this site
// with Hugo.
func (s *Site) GetEmbedCode(path string) string {
embedCode := fmt.Sprintf(embedFmt, path)
return embedCode
}