2017-06-24 19:12:15 +08:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2017-07-03 00:53:47 +08:00
|
|
|
"bytes"
|
2017-06-28 02:00:58 +08:00
|
|
|
"context"
|
2017-06-27 22:44:20 +08:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2017-07-25 18:57:27 +08:00
|
|
|
"fmt"
|
2017-06-27 22:44:20 +08:00
|
|
|
"hash"
|
|
|
|
"io"
|
2017-06-24 19:12:15 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-28 02:00:58 +08:00
|
|
|
"sort"
|
2017-06-24 19:12:15 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
"github.com/spf13/hugo/parser"
|
2017-06-24 19:12:15 +08:00
|
|
|
)
|
|
|
|
|
2017-06-27 22:44:20 +08:00
|
|
|
var (
|
|
|
|
errInvalidOption = errors.New("Invalid option")
|
|
|
|
)
|
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
// file contains the information about a particular file or directory.
|
|
|
|
type file struct {
|
2017-07-03 00:40:52 +08:00
|
|
|
// Indicates the Kind of view on the front-end (listing, editor or preview).
|
|
|
|
Kind string `json:"kind"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The name of the file.
|
2017-06-28 23:05:30 +08:00
|
|
|
Name string `json:"name"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The Size of the file.
|
2017-06-28 23:05:30 +08:00
|
|
|
Size int64 `json:"size"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The absolute URL.
|
2017-06-28 23:05:30 +08:00
|
|
|
URL string `json:"url"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The extension of the file.
|
2017-06-28 23:05:30 +08:00
|
|
|
Extension string `json:"extension"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The last modified time.
|
2017-06-28 23:05:30 +08:00
|
|
|
ModTime time.Time `json:"modified"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The File Mode.
|
2017-06-28 23:05:30 +08:00
|
|
|
Mode os.FileMode `json:"mode"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// Indicates if this file is a directory.
|
2017-06-28 23:05:30 +08:00
|
|
|
IsDir bool `json:"isDir"`
|
2017-06-25 21:24:26 +08:00
|
|
|
// Absolute path.
|
2017-06-28 23:05:30 +08:00
|
|
|
Path string `json:"path"`
|
2017-06-25 21:24:26 +08:00
|
|
|
// Relative path to user's virtual File System.
|
2017-06-28 23:05:30 +08:00
|
|
|
VirtualPath string `json:"virtualPath"`
|
2017-06-25 21:24:26 +08:00
|
|
|
// Indicates the file content type: video, text, image, music or blob.
|
2017-06-28 23:05:30 +08:00
|
|
|
Type string `json:"type"`
|
2017-06-28 18:45:41 +08:00
|
|
|
// Stores the content of a text file.
|
2017-06-29 17:17:35 +08:00
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
|
|
|
|
*listing `json:",omitempty"`
|
2017-07-01 15:50:42 +08:00
|
|
|
|
|
|
|
Metadata string `json:"metadata,omitempty"`
|
|
|
|
Language string `json:"language,omitempty"`
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 02:00:58 +08:00
|
|
|
// A listing is the context used to fill out a template.
|
|
|
|
type listing struct {
|
|
|
|
// The items (files and folders) in the path.
|
2017-07-08 19:46:19 +08:00
|
|
|
Items []*file `json:"items"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The number of directories in the listing.
|
2017-06-28 23:05:30 +08:00
|
|
|
NumDirs int `json:"numDirs"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// The number of files (items that aren't directories) in the listing.
|
2017-06-28 23:05:30 +08:00
|
|
|
NumFiles int `json:"numFiles"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// Which sorting order is used.
|
2017-06-28 23:05:30 +08:00
|
|
|
Sort string `json:"sort"`
|
2017-06-28 02:00:58 +08:00
|
|
|
// And which order.
|
2017-06-28 23:05:30 +08:00
|
|
|
Order string `json:"order"`
|
2017-06-29 17:17:35 +08:00
|
|
|
// Displays in mosaic or list.
|
|
|
|
Display string `json:"display"`
|
|
|
|
}
|
|
|
|
|
2017-06-25 20:03:59 +08:00
|
|
|
// getInfo gets the file information and, in case of error, returns the
|
2017-06-24 19:12:15 +08:00
|
|
|
// respective HTTP error code
|
2017-06-29 17:17:35 +08:00
|
|
|
func getInfo(url *url.URL, c *FileManager, u *User) (*file, error) {
|
2017-06-24 19:12:15 +08:00
|
|
|
var err error
|
|
|
|
|
2017-07-03 00:40:52 +08:00
|
|
|
i := &file{
|
2017-07-03 18:20:36 +08:00
|
|
|
URL: "/files" + url.Path,
|
2017-07-03 00:40:52 +08:00
|
|
|
VirtualPath: url.Path,
|
2017-07-03 15:59:49 +08:00
|
|
|
Path: filepath.Join(string(u.FileSystem), url.Path),
|
2017-07-03 00:40:52 +08:00
|
|
|
}
|
2017-06-24 19:12:15 +08:00
|
|
|
|
2017-07-03 15:59:49 +08:00
|
|
|
info, err := u.FileSystem.Stat(context.TODO(), url.Path)
|
2017-06-24 19:12:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Name = info.Name()
|
|
|
|
i.ModTime = info.ModTime()
|
|
|
|
i.Mode = info.Mode()
|
|
|
|
i.IsDir = info.IsDir()
|
|
|
|
i.Size = info.Size()
|
|
|
|
i.Extension = filepath.Ext(i.Name)
|
2017-07-03 22:19:17 +08:00
|
|
|
|
|
|
|
if i.IsDir && !strings.HasSuffix(i.URL, "/") {
|
|
|
|
i.URL += "/"
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:12:15 +08:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:00:58 +08:00
|
|
|
// getListing gets the information about a specific directory and its files.
|
2017-07-11 23:58:18 +08:00
|
|
|
func (i *file) getListing(c *RequestContext, r *http.Request) error {
|
2017-06-28 02:00:58 +08:00
|
|
|
// Gets the directory information using the Virtual File System of
|
|
|
|
// the user configuration.
|
2017-07-11 23:58:18 +08:00
|
|
|
f, err := c.User.FileSystem.OpenFile(context.TODO(), c.FI.VirtualPath, os.O_RDONLY, 0)
|
2017-06-28 02:00:58 +08:00
|
|
|
if err != nil {
|
2017-06-29 17:17:35 +08:00
|
|
|
return err
|
2017-06-28 02:00:58 +08:00
|
|
|
}
|
2017-06-29 17:17:35 +08:00
|
|
|
defer f.Close()
|
2017-06-28 02:00:58 +08:00
|
|
|
|
|
|
|
// Reads the directory and gets the information about the files.
|
2017-06-29 17:17:35 +08:00
|
|
|
files, err := f.Readdir(-1)
|
2017-06-28 02:00:58 +08:00
|
|
|
if err != nil {
|
2017-06-29 17:17:35 +08:00
|
|
|
return err
|
2017-06-28 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2017-07-08 19:46:19 +08:00
|
|
|
fileinfos []*file
|
2017-06-28 02:00:58 +08:00
|
|
|
dirCount, fileCount int
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
name := f.Name()
|
2017-07-11 23:58:18 +08:00
|
|
|
allowed := c.User.Allowed("/" + name)
|
2017-06-28 02:00:58 +08:00
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IsDir() {
|
|
|
|
name += "/"
|
|
|
|
dirCount++
|
|
|
|
} else {
|
|
|
|
fileCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute URL
|
2017-07-03 00:40:52 +08:00
|
|
|
url := url.URL{Path: i.URL + name}
|
2017-06-28 02:00:58 +08:00
|
|
|
|
2017-07-08 19:46:19 +08:00
|
|
|
i := &file{
|
|
|
|
Name: f.Name(),
|
|
|
|
Size: f.Size(),
|
|
|
|
ModTime: f.ModTime(),
|
|
|
|
Mode: f.Mode(),
|
|
|
|
IsDir: f.IsDir(),
|
|
|
|
URL: url.String(),
|
|
|
|
Extension: filepath.Ext(name),
|
|
|
|
VirtualPath: filepath.Join(i.VirtualPath, name),
|
|
|
|
Path: filepath.Join(i.Path, name),
|
2017-06-28 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:46:19 +08:00
|
|
|
i.RetrieveFileType(false)
|
2017-06-28 02:00:58 +08:00
|
|
|
fileinfos = append(fileinfos, i)
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
i.listing = &listing{
|
2017-06-28 02:00:58 +08:00
|
|
|
Items: fileinfos,
|
|
|
|
NumDirs: dirCount,
|
|
|
|
NumFiles: fileCount,
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getEditor gets the editor based on a Info struct
|
2017-07-03 00:40:52 +08:00
|
|
|
func (i *file) getEditor() error {
|
2017-07-01 15:50:42 +08:00
|
|
|
i.Language = editorLanguage(i.Extension)
|
|
|
|
// If the editor will hold only content, leave now.
|
|
|
|
if editorMode(i.Language) == "content" {
|
|
|
|
return nil
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-01 15:50:42 +08:00
|
|
|
// If the file doesn't have any kind of metadata, leave now.
|
2017-07-03 00:53:47 +08:00
|
|
|
if !hasRune(i.Content) {
|
2017-07-01 15:50:42 +08:00
|
|
|
return nil
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
buffer := bytes.NewBuffer([]byte(i.Content))
|
|
|
|
page, err := parser.ReadFrom(buffer)
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
// If there is an error, just ignore it and return nil.
|
|
|
|
// This way, the file can be served for editing.
|
|
|
|
if err != nil {
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
i.Content = strings.TrimSpace(string(page.Content()))
|
|
|
|
i.Metadata = strings.TrimSpace(string(page.FrontMatter()))
|
2017-06-29 17:17:35 +08:00
|
|
|
return nil
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
// RetrieveFileType obtains the mimetype and converts it to a simple
|
|
|
|
// type nomenclature.
|
2017-07-08 19:46:19 +08:00
|
|
|
func (i *file) RetrieveFileType(checkContent bool) error {
|
2017-06-29 17:17:35 +08:00
|
|
|
var content []byte
|
|
|
|
var err error
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
// Tries to get the file mimetype using its extension.
|
|
|
|
mimetype := mime.TypeByExtension(i.Extension)
|
2017-06-24 19:12:15 +08:00
|
|
|
|
2017-07-08 19:46:19 +08:00
|
|
|
if mimetype == "" && checkContent {
|
2017-07-10 15:43:46 +08:00
|
|
|
file, err := os.Open(i.Path)
|
2017-06-24 19:12:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-10 15:43:46 +08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Only the first 512 bytes are used to sniff the content type.
|
|
|
|
buffer := make([]byte, 512)
|
|
|
|
_, err = file.Read(buffer)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-24 19:12:15 +08:00
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
// Tries to get the file mimetype using its first
|
|
|
|
// 512 bytes.
|
2017-07-10 15:43:46 +08:00
|
|
|
mimetype = http.DetectContentType(buffer)
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
if strings.HasPrefix(mimetype, "video") {
|
2017-06-24 19:12:15 +08:00
|
|
|
i.Type = "video"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
if strings.HasPrefix(mimetype, "audio") {
|
2017-06-24 19:12:15 +08:00
|
|
|
i.Type = "audio"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
if strings.HasPrefix(mimetype, "image") {
|
2017-06-24 19:12:15 +08:00
|
|
|
i.Type = "image"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
if strings.HasPrefix(mimetype, "text") {
|
2017-06-24 19:12:15 +08:00
|
|
|
i.Type = "text"
|
2017-06-29 17:17:35 +08:00
|
|
|
goto End
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 21:24:26 +08:00
|
|
|
if strings.HasPrefix(mimetype, "application/javascript") {
|
2017-06-24 19:12:15 +08:00
|
|
|
i.Type = "text"
|
2017-06-29 17:17:35 +08:00
|
|
|
goto End
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the type isn't text (and is blob for example), it will check some
|
|
|
|
// common types that are mistaken not to be text.
|
|
|
|
for _, extension := range textExtensions {
|
|
|
|
if strings.HasSuffix(i.Name, extension) {
|
|
|
|
i.Type = "text"
|
2017-07-10 15:43:46 +08:00
|
|
|
goto End
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Type = "blob"
|
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
End:
|
|
|
|
// If the file type is text, save its content.
|
|
|
|
if i.Type == "text" {
|
|
|
|
if len(content) == 0 {
|
|
|
|
content, err = ioutil.ReadFile(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 19:12:15 +08:00
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
i.Content = string(content)
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-06-24 19:12:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
func (i file) Checksum(kind string) (string, error) {
|
2017-06-27 22:44:20 +08:00
|
|
|
file, err := os.Open(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var h hash.Hash
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case "md5":
|
|
|
|
h = md5.New()
|
|
|
|
case "sha1":
|
|
|
|
h = sha1.New()
|
|
|
|
case "sha256":
|
|
|
|
h = sha256.New()
|
|
|
|
case "sha512":
|
|
|
|
h = sha512.New()
|
|
|
|
default:
|
|
|
|
return "", errInvalidOption
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(h, file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:12:15 +08:00
|
|
|
// CanBeEdited checks if the extension of a file is supported by the editor
|
2017-06-29 17:17:35 +08:00
|
|
|
func (i file) CanBeEdited() bool {
|
2017-06-24 19:12:15 +08:00
|
|
|
return i.Type == "text"
|
|
|
|
}
|
2017-06-28 02:00:58 +08:00
|
|
|
|
|
|
|
// ApplySort applies the sort order using .Order and .Sort
|
|
|
|
func (l listing) ApplySort() {
|
|
|
|
// Check '.Order' to know how to sort
|
|
|
|
if l.Order == "desc" {
|
|
|
|
switch l.Sort {
|
|
|
|
case "name":
|
|
|
|
sort.Sort(sort.Reverse(byName(l)))
|
|
|
|
case "size":
|
|
|
|
sort.Sort(sort.Reverse(bySize(l)))
|
|
|
|
default:
|
|
|
|
// If not one of the above, do nothing
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else { // If we had more Orderings we could add them here
|
|
|
|
switch l.Sort {
|
|
|
|
case "name":
|
|
|
|
sort.Sort(byName(l))
|
|
|
|
case "size":
|
|
|
|
sort.Sort(bySize(l))
|
|
|
|
default:
|
|
|
|
sort.Sort(byName(l))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement sorting for listing
|
|
|
|
type byName listing
|
|
|
|
type bySize listing
|
|
|
|
|
|
|
|
// By Name
|
|
|
|
func (l byName) Len() int {
|
|
|
|
return len(l.Items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l byName) Swap(i, j int) {
|
|
|
|
l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat upper and lower case equally
|
|
|
|
func (l byName) Less(i, j int) bool {
|
|
|
|
if l.Items[i].IsDir && !l.Items[j].IsDir {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !l.Items[i].IsDir && l.Items[j].IsDir {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// By Size
|
|
|
|
func (l bySize) Len() int {
|
|
|
|
return len(l.Items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l bySize) Swap(i, j int) {
|
|
|
|
l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
const directoryOffset = -1 << 31 // = math.MinInt32
|
|
|
|
func (l bySize) Less(i, j int) bool {
|
|
|
|
iSize, jSize := l.Items[i].Size, l.Items[j].Size
|
|
|
|
if l.Items[i].IsDir {
|
|
|
|
iSize = directoryOffset + iSize
|
|
|
|
}
|
|
|
|
if l.Items[j].IsDir {
|
|
|
|
jSize = directoryOffset + jSize
|
|
|
|
}
|
|
|
|
return iSize < jSize
|
|
|
|
}
|
|
|
|
|
|
|
|
var textExtensions = [...]string{
|
|
|
|
".md", ".markdown", ".mdown", ".mmark",
|
|
|
|
".asciidoc", ".adoc", ".ad",
|
|
|
|
".rst",
|
|
|
|
".json", ".toml", ".yaml", ".csv", ".xml", ".rss", ".conf", ".ini",
|
|
|
|
".tex", ".sty",
|
|
|
|
".css", ".sass", ".scss",
|
|
|
|
".js",
|
|
|
|
".html",
|
|
|
|
".txt", ".rtf",
|
|
|
|
".sh", ".bash", ".ps1", ".bat", ".cmd",
|
|
|
|
".php", ".pl", ".py",
|
|
|
|
"Caddyfile",
|
|
|
|
".c", ".cc", ".h", ".hh", ".cpp", ".hpp", ".f90",
|
|
|
|
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
|
|
|
|
}
|
2017-06-29 17:17:35 +08:00
|
|
|
|
2017-07-03 00:53:47 +08:00
|
|
|
// hasRune checks if the file has the frontmatter rune
|
|
|
|
func hasRune(file string) bool {
|
|
|
|
return strings.HasPrefix(file, "---") ||
|
|
|
|
strings.HasPrefix(file, "+++") ||
|
|
|
|
strings.HasPrefix(file, "{")
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:17:35 +08:00
|
|
|
func editorMode(language string) string {
|
|
|
|
switch language {
|
|
|
|
case "markdown", "asciidoc", "rst":
|
2017-07-01 15:50:42 +08:00
|
|
|
return "content+metadata"
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-01 15:50:42 +08:00
|
|
|
return "content"
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func editorLanguage(mode string) string {
|
2017-07-01 01:03:08 +08:00
|
|
|
mode = strings.TrimPrefix(mode, ".")
|
2017-06-29 17:17:35 +08:00
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "md", "markdown", "mdown", "mmark":
|
|
|
|
mode = "markdown"
|
2017-07-01 01:03:08 +08:00
|
|
|
case "yml":
|
|
|
|
mode = "yaml"
|
2017-06-29 17:17:35 +08:00
|
|
|
case "asciidoc", "adoc", "ad":
|
|
|
|
mode = "asciidoc"
|
|
|
|
case "rst":
|
|
|
|
mode = "rst"
|
2017-07-11 23:58:18 +08:00
|
|
|
case "html", "htm", "xml":
|
|
|
|
mode = "htmlmixed"
|
2017-06-29 17:17:35 +08:00
|
|
|
case "js":
|
|
|
|
mode = "javascript"
|
|
|
|
case "go":
|
|
|
|
mode = "golang"
|
2017-07-11 23:58:18 +08:00
|
|
|
case "":
|
|
|
|
mode = "text"
|
2017-06-29 17:17:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return mode
|
|
|
|
}
|
2017-07-25 18:57:27 +08:00
|
|
|
|
|
|
|
func copyFile(source string, dest string) (err error) {
|
|
|
|
sourcefile, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer sourcefile.Close()
|
|
|
|
|
|
|
|
destfile, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer destfile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(destfile, sourcefile)
|
|
|
|
if err == nil {
|
|
|
|
sourceinfo, err := os.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
err = os.Chmod(dest, sourceinfo.Mode())
|
2017-07-25 19:25:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-25 18:57:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDir(source string, dest string) (err error) {
|
|
|
|
// get properties of source dir
|
|
|
|
sourceinfo, err := os.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create dest dir
|
|
|
|
err = os.MkdirAll(dest, sourceinfo.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
directory, _ := os.Open(source)
|
|
|
|
objects, err := directory.Readdir(-1)
|
|
|
|
|
|
|
|
for _, obj := range objects {
|
|
|
|
sourcefilepointer := source + "/" + obj.Name()
|
|
|
|
destinationfilepointer := dest + "/" + obj.Name()
|
|
|
|
|
|
|
|
if obj.IsDir() {
|
|
|
|
// create sub-directories - recursively
|
|
|
|
err = copyDir(sourcefilepointer, destinationfilepointer)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// perform copy
|
|
|
|
err = copyFile(sourcefilepointer, destinationfilepointer)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|