mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Replace utils/file with go-fs and go-installer, fix previous tests
This commit is contained in:
parent
1d49188da9
commit
6163726bc5
12
context.go
12
context.go
|
@ -21,6 +21,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/go-fs"
|
||||
|
||||
"github.com/iris-contrib/formBinder"
|
||||
"github.com/kataras/go-errors"
|
||||
"github.com/kataras/iris/config"
|
||||
|
@ -405,12 +407,12 @@ func (ctx *Context) ReadForm(formObject interface{}) error {
|
|||
multipartForm, err := reqCtx.MultipartForm()
|
||||
if err == nil {
|
||||
//we have multipart form
|
||||
return errReadBody.Format(formBinder.Decode(multipartForm.Value, formObject))
|
||||
return errReadBody.With(formBinder.Decode(multipartForm.Value, formObject))
|
||||
}
|
||||
// if no multipart and post arguments ( means normal form)
|
||||
|
||||
if reqCtx.PostArgs().Len() == 0 && reqCtx.QueryArgs().Len() == 0 {
|
||||
return errReadBody.Format(errNoForm)
|
||||
return errReadBody.With(errNoForm)
|
||||
}
|
||||
|
||||
form := make(map[string][]string, reqCtx.PostArgs().Len()+reqCtx.QueryArgs().Len())
|
||||
|
@ -438,7 +440,7 @@ func (ctx *Context) ReadForm(formObject interface{}) error {
|
|||
}
|
||||
})
|
||||
|
||||
return errReadBody.Format(formBinder.Decode(form, formObject))
|
||||
return errReadBody.With(formBinder.Decode(form, formObject))
|
||||
}
|
||||
|
||||
/* Response */
|
||||
|
@ -637,7 +639,7 @@ func (ctx *Context) ServeContent(content io.ReadSeeker, filename string, modtime
|
|||
return nil
|
||||
}
|
||||
|
||||
ctx.RequestCtx.Response.Header.Set(contentType, utils.TypeByExtension(filename))
|
||||
ctx.RequestCtx.Response.Header.Set(contentType, fs.TypeByExtension(filename))
|
||||
ctx.RequestCtx.Response.Header.Set(lastModified, modtime.UTC().Format(config.TimeFormat))
|
||||
ctx.RequestCtx.SetStatusCode(StatusOK)
|
||||
var out io.Writer
|
||||
|
@ -654,7 +656,7 @@ func (ctx *Context) ServeContent(content io.ReadSeeker, filename string, modtime
|
|||
|
||||
}
|
||||
_, err := io.Copy(out, content)
|
||||
return errServeContent.Format(err)
|
||||
return errServeContent.With(err)
|
||||
}
|
||||
|
||||
// ServeFile serves a view file, to send a file ( zip for example) to the client you should use the SendFile(serverfilename,clientfilename)
|
||||
|
|
9
iris.go
9
iris.go
|
@ -77,6 +77,7 @@ import (
|
|||
"github.com/iris-contrib/response/xml"
|
||||
"github.com/iris-contrib/template/html"
|
||||
"github.com/kataras/go-errors"
|
||||
"github.com/kataras/go-fs"
|
||||
"github.com/kataras/iris/config"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/utils"
|
||||
|
@ -1724,7 +1725,7 @@ func (api *muxAPI) StaticServe(systemPath string, requestPath ...string) RouteNa
|
|||
var reqPath string
|
||||
|
||||
if len(requestPath) == 0 {
|
||||
reqPath = strings.Replace(systemPath, utils.PathSeparator, slash, -1) // replaces any \ to /
|
||||
reqPath = strings.Replace(systemPath, fs.PathSeparator, slash, -1) // replaces any \ to /
|
||||
reqPath = strings.Replace(reqPath, "//", slash, -1) // for any case, replaces // to /
|
||||
reqPath = strings.Replace(reqPath, ".", "", -1) // replace any dots (./mypath -> /mypath)
|
||||
} else {
|
||||
|
@ -1734,10 +1735,10 @@ func (api *muxAPI) StaticServe(systemPath string, requestPath ...string) RouteNa
|
|||
return api.Get(reqPath+"/*file", func(ctx *Context) {
|
||||
filepath := ctx.Param("file")
|
||||
|
||||
spath := strings.Replace(filepath, "/", utils.PathSeparator, -1)
|
||||
spath := strings.Replace(filepath, "/", fs.PathSeparator, -1)
|
||||
spath = path.Join(systemPath, spath)
|
||||
|
||||
if !utils.DirectoryExists(spath) {
|
||||
if !fs.DirectoryExists(spath) {
|
||||
ctx.NotFound()
|
||||
return
|
||||
}
|
||||
|
@ -1816,7 +1817,7 @@ func (api *muxAPI) Favicon(favPath string, requestPath ...string) RouteNameFunc
|
|||
fi, _ = f.Stat()
|
||||
}
|
||||
modtime := fi.ModTime().UTC().Format(config.TimeFormat)
|
||||
cType := utils.TypeByExtension(favPath)
|
||||
cType := fs.TypeByExtension(favPath)
|
||||
// copy the bytes here in order to cache and not read the ico on each request.
|
||||
cacheFav := make([]byte, fi.Size())
|
||||
if _, err = f.Read(cacheFav); err != nil {
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/kataras/cli"
|
||||
"github.com/kataras/go-fs"
|
||||
"github.com/kataras/go-installer"
|
||||
"github.com/kataras/iris/utils"
|
||||
)
|
||||
|
||||
|
@ -19,7 +21,7 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
packagesInstallDir = utils.AssetsDirectory + utils.PathSeparator + "iris-command-assets" + utils.PathSeparator
|
||||
packagesInstallDir = utils.AssetsDirectory + fs.PathSeparator + "iris-command-assets" + fs.PathSeparator
|
||||
// packages should install with go get before create the package
|
||||
packagesDependencies = []string{"github.com/iris-contrib/middleware/logger"}
|
||||
)
|
||||
|
@ -36,7 +38,7 @@ func isValidInstallDir(targetDir string) bool {
|
|||
gopaths := strings.Split(gopath, string(os.PathListSeparator))
|
||||
// the package MUST be installed only inside a valid gopath, if not then print an error to the user.
|
||||
for _, gpath := range gopaths {
|
||||
if strings.HasPrefix(targetDir, gpath+utils.PathSeparator) {
|
||||
if strings.HasPrefix(targetDir, gpath+fs.PathSeparator) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +57,7 @@ func create(flags cli.Flags) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
if !utils.DirectoryExists(packagesInstallDir) || !flags.Bool("offline") {
|
||||
if !fs.DirectoryExists(packagesInstallDir) || !flags.Bool("offline") {
|
||||
// install/update go dependencies at the same time downloading the zip from the github iris-contrib assets
|
||||
finish := make(chan bool)
|
||||
go func() {
|
||||
|
@ -84,7 +86,7 @@ func create(flags cli.Flags) (err error) {
|
|||
func downloadPackages() {
|
||||
errMsg := "\nProblem while downloading the assets from the internet for the first time. Trace: %s"
|
||||
|
||||
installedDir, err := utils.Install(PackagesURL, packagesInstallDir)
|
||||
installedDir, err := installer.Install(PackagesURL, packagesInstallDir, true)
|
||||
if err != nil {
|
||||
printer.Dangerf(errMsg, err.Error())
|
||||
return
|
||||
|
@ -92,7 +94,7 @@ func downloadPackages() {
|
|||
|
||||
// installedDir is the packagesInstallDir+PackagesExportedName, we will copy these contents to the parent, to the packagesInstallDir, because of import paths.
|
||||
|
||||
err = utils.CopyDir(installedDir, packagesInstallDir)
|
||||
err = fs.CopyDir(installedDir, packagesInstallDir)
|
||||
if err != nil {
|
||||
printer.Dangerf(errMsg, err.Error())
|
||||
return
|
||||
|
@ -101,14 +103,14 @@ func downloadPackages() {
|
|||
// we don't exit on errors here.
|
||||
|
||||
// try to remove the unzipped folder
|
||||
utils.RemoveFile(installedDir[0 : len(installedDir)-1])
|
||||
fs.RemoveFile(installedDir[0 : len(installedDir)-1])
|
||||
}
|
||||
|
||||
func createPackage(packageName string, targetDir string) error {
|
||||
installTo := targetDir // os.Getenv("GOPATH") + utils.PathSeparator + "src" + utils.PathSeparator + targetDir
|
||||
installTo := targetDir // os.Getenv("GOPATH") + fs.PathSeparator + "src" + fs.PathSeparator + targetDir
|
||||
|
||||
packageDir := packagesInstallDir + utils.PathSeparator + packageName
|
||||
err := utils.CopyDir(packageDir, installTo)
|
||||
packageDir := packagesInstallDir + fs.PathSeparator + packageName
|
||||
err := fs.CopyDir(packageDir, installTo)
|
||||
if err != nil {
|
||||
printer.Dangerf("\nProblem while copying the %s package to the %s. Trace: %s", packageName, installTo, err.Error())
|
||||
return err
|
||||
|
@ -117,7 +119,7 @@ func createPackage(packageName string, targetDir string) error {
|
|||
// now replace main.go's 'github.com/iris-contrib/iris-command-assets/basic/' with targetDir
|
||||
// hardcode all that, we don't have anything special and neither will do
|
||||
targetDir = strings.Replace(targetDir, "\\", "/", -1) // for any case
|
||||
mainFile := installTo + utils.PathSeparator + "backend" + utils.PathSeparator + "main.go"
|
||||
mainFile := installTo + fs.PathSeparator + "backend" + fs.PathSeparator + "main.go"
|
||||
|
||||
input, err := ioutil.ReadFile(mainFile)
|
||||
if err != nil {
|
||||
|
@ -138,7 +140,7 @@ func createPackage(packageName string, targetDir string) error {
|
|||
// go build
|
||||
buildCmd := utils.CommandBuilder("go", "build")
|
||||
if installTo[len(installTo)-1] != os.PathSeparator || installTo[len(installTo)-1] != '/' {
|
||||
installTo += utils.PathSeparator
|
||||
installTo += fs.PathSeparator
|
||||
}
|
||||
buildCmd.Dir = installTo + "backend"
|
||||
buildCmd.Stderr = os.Stderr
|
||||
|
@ -155,7 +157,7 @@ func createPackage(packageName string, targetDir string) error {
|
|||
executable += ".exe"
|
||||
}
|
||||
|
||||
runCmd := utils.CommandBuilder("." + utils.PathSeparator + executable)
|
||||
runCmd := utils.CommandBuilder("." + fs.PathSeparator + executable)
|
||||
runCmd.Dir = buildCmd.Dir
|
||||
runCmd.Stdout = os.Stdout
|
||||
runCmd.Stderr = os.Stderr
|
||||
|
|
13
plugin.go
13
plugin.go
|
@ -6,7 +6,8 @@ import (
|
|||
"github.com/kataras/go-errors"
|
||||
|
||||
"github.com/iris-contrib/logger"
|
||||
"github.com/kataras/iris/utils"
|
||||
"github.com/kataras/go-fs"
|
||||
"github.com/kataras/go-installer"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -199,27 +200,27 @@ var _ PluginContainer = &pluginContainer{}
|
|||
|
||||
// DirectoryExists returns true if a given local directory exists
|
||||
func (d *pluginDownloadManager) DirectoryExists(dir string) bool {
|
||||
return utils.DirectoryExists(dir)
|
||||
return fs.DirectoryExists(dir)
|
||||
}
|
||||
|
||||
// DownloadZip downlodas a zip to the given local path location
|
||||
func (d *pluginDownloadManager) DownloadZip(zipURL string, targetDir string) (string, error) {
|
||||
return utils.DownloadZip(zipURL, targetDir)
|
||||
return installer.DownloadZip(zipURL, targetDir, true)
|
||||
}
|
||||
|
||||
// Unzip unzips a zip to the given local path location
|
||||
func (d *pluginDownloadManager) Unzip(archive string, target string) (string, error) {
|
||||
return utils.Unzip(archive, target)
|
||||
return installer.DownloadZip(archive, target, true)
|
||||
}
|
||||
|
||||
// Remove deletes/removes/rm a file
|
||||
func (d *pluginDownloadManager) Remove(filePath string) error {
|
||||
return utils.RemoveFile(filePath)
|
||||
return fs.RemoveFile(filePath)
|
||||
}
|
||||
|
||||
// Install is just the flow of the: DownloadZip->Unzip->Remove the zip
|
||||
func (d *pluginDownloadManager) Install(remoteFileZip string, targetDirectory string) (string, error) {
|
||||
return utils.Install(remoteFileZip, targetDirectory)
|
||||
return installer.Install(remoteFileZip, targetDirectory, true)
|
||||
}
|
||||
|
||||
// pluginContainer is the base container of all Iris, registed plugins
|
||||
|
|
4
ssh.go
4
ssh.go
|
@ -47,7 +47,7 @@ import (
|
|||
"github.com/kardianos/osext"
|
||||
"github.com/kardianos/service"
|
||||
"github.com/kataras/go-errors"
|
||||
"github.com/kataras/iris/utils"
|
||||
"github.com/kataras/go-fs"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
@ -324,7 +324,7 @@ func generateSigner(keypath string, sshKeygenBin string) (ssh.Signer, error) {
|
|||
} else {
|
||||
sshKeygenBin = "ssh-keygen"
|
||||
}
|
||||
if !utils.DirectoryExists(keypath) {
|
||||
if !fs.DirectoryExists(keypath) {
|
||||
os.MkdirAll(filepath.Dir(keypath), os.ModePerm)
|
||||
keygenCmd := exec.Command(sshKeygenBin, "-f", keypath, "-t", "rsa", "-N", "")
|
||||
_, err := keygenCmd.Output()
|
||||
|
|
331
utils/file.go
331
utils/file.go
|
@ -1,331 +0,0 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// ContentBINARY is the string of "application/octet-stream response headers
|
||||
ContentBINARY = "application/octet-stream"
|
||||
)
|
||||
|
||||
var (
|
||||
// AssetsDirectory the path which iris saves some assets came from the internet ( used in iris control plugin (to download the html,css,js) and for iris command line tool to download the packages)
|
||||
AssetsDirectory = ""
|
||||
)
|
||||
|
||||
// init just sets the iris path for assets, used in iris control plugin and for iris command line tool(create command)
|
||||
// the AssetsDirectory path should be like: C:/users/kataras/.iris (for windows) and for linux you can imagine
|
||||
func init() {
|
||||
homepath := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
homepath = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||
} else {
|
||||
homepath = os.Getenv("HOME")
|
||||
}
|
||||
AssetsDirectory = homepath + PathSeparator + ".iris"
|
||||
}
|
||||
|
||||
// DirectoryExists returns true if a directory(or file) exists, otherwise false
|
||||
func DirectoryExists(dir string) bool {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// DownloadZip downloads a zip file returns the downloaded filename and an error.
|
||||
//
|
||||
// An indicator is always shown up to the terminal, so the user will know if (a plugin) try to download something
|
||||
func DownloadZip(zipURL string, newDir string) (string, error) {
|
||||
var err error
|
||||
var size int64
|
||||
finish := make(chan bool)
|
||||
defer func() {
|
||||
finish <- true
|
||||
}()
|
||||
|
||||
go func() {
|
||||
print("\n|")
|
||||
print("_")
|
||||
print("|")
|
||||
|
||||
for {
|
||||
select {
|
||||
case v := <-finish:
|
||||
{
|
||||
if v {
|
||||
print("\010\010\010") //remove the loading chars
|
||||
close(finish)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
default:
|
||||
print("\010\010-")
|
||||
time.Sleep(time.Second / 2)
|
||||
print("\010\\")
|
||||
time.Sleep(time.Second / 2)
|
||||
print("\010|")
|
||||
time.Sleep(time.Second / 2)
|
||||
print("\010/")
|
||||
time.Sleep(time.Second / 2)
|
||||
print("\010-")
|
||||
time.Sleep(time.Second / 2)
|
||||
print("|")
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
os.MkdirAll(newDir, 0755) // os.ModeDir, 0755 fix for unix users by @cgyy
|
||||
tokens := strings.Split(zipURL, "/")
|
||||
fileName := newDir + tokens[len(tokens)-1]
|
||||
if !strings.HasSuffix(fileName, ".zip") {
|
||||
return "", ErrNoZip.Format(fileName)
|
||||
}
|
||||
|
||||
output, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
return "", ErrFileCreate.Format(err.Error())
|
||||
}
|
||||
defer output.Close()
|
||||
response, err := http.Get(zipURL)
|
||||
if err != nil {
|
||||
return "", ErrFileDownload.Format(zipURL, err.Error())
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
size, err = io.Copy(output, response.Body)
|
||||
if err != nil {
|
||||
return "", ErrFileCopy.Format(err.Error())
|
||||
}
|
||||
|
||||
_ = size
|
||||
//print("OK ", size, " bytes downloaded") //we keep that here so developer will always see in the terminal if a plugin downloads something or no ?
|
||||
return fileName, nil
|
||||
|
||||
}
|
||||
|
||||
// Unzip extracts a zipped file to the target location
|
||||
//
|
||||
// it removes the zipped file after successfully completion
|
||||
// returns a string with the path of the created folder (if any) and an error (if any)
|
||||
func Unzip(archive string, target string) (string, error) {
|
||||
reader, err := zip.OpenReader(archive)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(target, 0755); err != nil {
|
||||
return "", ErrDirCreate.Format(target, err.Error())
|
||||
}
|
||||
createdFolder := ""
|
||||
for _, file := range reader.File {
|
||||
path := filepath.Join(target, file.Name)
|
||||
if file.FileInfo().IsDir() {
|
||||
os.MkdirAll(path, file.Mode())
|
||||
if createdFolder == "" {
|
||||
// this is the new directory that zip has
|
||||
createdFolder = path
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
fileReader, err := file.Open()
|
||||
if err != nil {
|
||||
return "", ErrFileOpen.Format(err.Error())
|
||||
}
|
||||
defer fileReader.Close()
|
||||
|
||||
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
|
||||
if err != nil {
|
||||
return "", ErrFileOpen.Format(err.Error())
|
||||
}
|
||||
defer targetFile.Close()
|
||||
|
||||
if _, err := io.Copy(targetFile, fileReader); err != nil {
|
||||
return "", ErrFileCopy.Format(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
reader.Close()
|
||||
return createdFolder, nil
|
||||
}
|
||||
|
||||
// RemoveFile removes a file or directory and returns an error, if any
|
||||
func RemoveFile(filePath string) error {
|
||||
return ErrFileRemove.Format(os.RemoveAll(filePath))
|
||||
}
|
||||
|
||||
// Install is just the flow of: downloadZip -> unzip -> removeFile(zippedFile)
|
||||
// accepts 2 parameters
|
||||
//
|
||||
// first parameter is the remote url file zip
|
||||
// second parameter is the target directory
|
||||
// returns a string(installedDirectory) and an error
|
||||
//
|
||||
// (string) installedDirectory is the directory which the zip file had, this is the real installation path, you don't need to know what it's because these things maybe change to the future let's keep it to return the correct path.
|
||||
// the installedDirectory is not empty when the installation is succed, the targetDirectory is not already exists and no error happens
|
||||
// the installedDirectory is empty when the installation is already done by previous time or an error happens
|
||||
func Install(remoteFileZip string, targetDirectory string) (installedDirectory string, err error) {
|
||||
var zipFile string
|
||||
|
||||
zipFile, err = DownloadZip(remoteFileZip, targetDirectory)
|
||||
if err == nil {
|
||||
installedDirectory, err = Unzip(zipFile, targetDirectory)
|
||||
if err == nil {
|
||||
installedDirectory += string(os.PathSeparator)
|
||||
RemoveFile(zipFile)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CopyFile copy a file, accepts full path of the source and full path of destination, if file exists it's overrides it
|
||||
// this function doesn't checks for permissions and all that, it returns an error if didn't worked
|
||||
func CopyFile(source string, destination string) error {
|
||||
reader, err := os.Open(source)
|
||||
|
||||
if err != nil {
|
||||
return ErrFileOpen.Format(err.Error())
|
||||
}
|
||||
|
||||
defer reader.Close()
|
||||
|
||||
writer, err := os.Create(destination)
|
||||
if err != nil {
|
||||
return ErrFileCreate.Format(err.Error())
|
||||
}
|
||||
|
||||
defer writer.Close()
|
||||
|
||||
_, err = io.Copy(writer, reader)
|
||||
if err != nil {
|
||||
return ErrFileCopy.Format(err.Error())
|
||||
}
|
||||
|
||||
err = writer.Sync()
|
||||
if err != nil {
|
||||
return ErrFileCopy.Format(err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
|
||||
// Source directory must exist.
|
||||
//
|
||||
// Note: the CopyDir function was not written by me, but its working well
|
||||
func CopyDir(source string, dest string) (err error) {
|
||||
|
||||
// get properties of source dir
|
||||
fi, err := os.Stat(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !fi.IsDir() {
|
||||
return fmt.Errorf("Source is not a directory! Source path: %s", source)
|
||||
}
|
||||
|
||||
/*_, err = os.Open(dest)
|
||||
if !os.IsNotExist(err) {
|
||||
return nil // Destination already exists
|
||||
}*/
|
||||
|
||||
// create dest dir
|
||||
|
||||
err = os.MkdirAll(dest, fi.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entries, err := ioutil.ReadDir(source)
|
||||
|
||||
for _, entry := range entries {
|
||||
|
||||
sfp := source + "/" + entry.Name()
|
||||
dfp := dest + "/" + entry.Name()
|
||||
if entry.IsDir() {
|
||||
err = CopyDir(sfp, dfp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// perform copy
|
||||
err = CopyFile(sfp, dfp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// TypeByExtension returns the MIME type associated with the file extension ext.
|
||||
// The extension ext should begin with a leading dot, as in ".html".
|
||||
// When ext has no associated type, TypeByExtension returns "".
|
||||
//
|
||||
// Extensions are looked up first case-sensitively, then case-insensitively.
|
||||
//
|
||||
// The built-in table is small but on unix it is augmented by the local
|
||||
// system's mime.types file(s) if available under one or more of these
|
||||
// names:
|
||||
//
|
||||
// /etc/mime.types
|
||||
// /etc/apache2/mime.types
|
||||
// /etc/apache/mime.types
|
||||
//
|
||||
// On Windows, MIME types are extracted from the registry.
|
||||
//
|
||||
// Text types have the charset parameter set to "utf-8" by default.
|
||||
func TypeByExtension(fullfilename string) (t string) {
|
||||
ext := filepath.Ext(fullfilename)
|
||||
//these should be found by the windows(registry) and unix(apache) but on windows some machines have problems on this part.
|
||||
if t = mime.TypeByExtension(ext); t == "" {
|
||||
// no use of map here because we will have to lock/unlock it, by hand is better, no problem:
|
||||
if ext == ".json" {
|
||||
t = "application/json"
|
||||
} else if ext == ".zip" {
|
||||
t = "application/zip"
|
||||
} else if ext == ".3gp" {
|
||||
t = "video/3gpp"
|
||||
} else if ext == ".7z" {
|
||||
t = "application/x-7z-compressed"
|
||||
} else if ext == ".ace" {
|
||||
t = "application/x-ace-compressed"
|
||||
} else if ext == ".aac" {
|
||||
t = "audio/x-aac"
|
||||
} else if ext == ".ico" { // for any case
|
||||
t = "image/x-icon"
|
||||
} else {
|
||||
t = ContentBINARY
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetParentDir returns the parent directory(string) of the passed targetDirectory (string)
|
||||
func GetParentDir(targetDirectory string) string {
|
||||
lastSlashIndex := strings.LastIndexByte(targetDirectory, os.PathSeparator)
|
||||
//check if the slash is at the end , if yes then re- check without the last slash, we don't want /path/to/ , we want /path/to in order to get the /path/ which is the parent directory of the /path/to
|
||||
if lastSlashIndex == len(targetDirectory)-1 {
|
||||
lastSlashIndex = strings.LastIndexByte(targetDirectory[0:lastSlashIndex], os.PathSeparator)
|
||||
}
|
||||
|
||||
parentDirectory := targetDirectory[0:lastSlashIndex]
|
||||
return parentDirectory
|
||||
}
|
28
utils/installer.go
Normal file
28
utils/installer.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
const (
|
||||
// ContentBINARY is the string of "application/octet-stream response headers
|
||||
ContentBINARY = "application/octet-stream"
|
||||
)
|
||||
|
||||
var (
|
||||
// AssetsDirectory the path which iris saves some assets came from the internet ( used in iris control plugin (to download the html,css,js) and for iris command line tool to download the packages)
|
||||
AssetsDirectory = ""
|
||||
)
|
||||
|
||||
// init just sets the iris path for assets, used in iris control plugin and for iris command line tool(create command)
|
||||
// the AssetsDirectory path should be like: C:/users/kataras/.iris (for windows) and for linux you can imagine
|
||||
func init() {
|
||||
homepath := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
homepath = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||
} else {
|
||||
homepath = os.Getenv("HOME")
|
||||
}
|
||||
AssetsDirectory = homepath + PathSeparator + ".iris"
|
||||
}
|
Loading…
Reference in New Issue
Block a user