Extract some stuff out

This commit is contained in:
Olivier Tremblay 2018-12-11 15:14:26 -05:00
parent 8675e6815a
commit 46286cb851
No known key found for this signature in database
GPG key ID: D1C73ACB855E3A6D
3 changed files with 199 additions and 114 deletions

89
filereq.go Normal file
View file

@ -0,0 +1,89 @@
package sharethis // import "github.com/otremblay/sharethis"
import (
"crypto/sha256"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"golang.org/x/crypto/ssh"
)
type FileReq struct {
Path string
ShareCount uint
Username string
Hostname string
ServerConn *ssh.ServerConn
fileurl string
httpport string
remotehost string
localpath string
filename string
IsDir bool
}
func NewFileReq(path, httpport, remotehost string, shareCount uint) *FileReq {
return getFileReq(DefaultPrefixFn, path, httpport, remotehost, shareCount)
}
func (fr *FileReq) RebuildUrls() {
fr = getFileReq(DefaultPrefixFn, fr.localpath, fr.httpport, fr.remotehost, fr.ShareCount)
}
func (fr *FileReq) String() string {
if fr.IsDir {
return fmt.Sprintf("%s\n%s", fr.fileurl+".tar", fr.fileurl+".zip")
} else {
return fmt.Sprintf(fr.fileurl)
}
}
func getRemotePath(prefixFn func() string, path string) string {
fullpath := fmt.Sprintf("%s:%s", prefixFn(), path)
hashedpath := fmt.Sprintf("%x", sha256.Sum256([]byte(fullpath)))
return fmt.Sprintf("%s/%s", hashedpath, filepath.Base(path))
}
func getFileReq(prefixFn func() string, path string, httpport string, remotehost string, sharecount uint) *FileReq {
var dir bool
if fs, err := os.Stat(path); err != nil {
log.Fatalln("Can't read file")
} else {
p, err := filepath.Abs(fs.Name())
if err != nil {
log.Fatalln("Can't read file")
}
dir = fs.IsDir()
path = p
}
remotepath := getRemotePath(prefixFn, path)
var fileurl string
if httpport == "443" {
fileurl = fmt.Sprintf("https://%s/%s", remotehost, remotepath)
} else if httpport == "80" {
fileurl = fmt.Sprintf("http://%s/%s", remotehost, remotepath)
} else {
fileurl = fmt.Sprintf("http://%s:%s/%s", remotehost, httpport, remotepath)
}
return &FileReq{Path: remotepath, ShareCount: sharecount, fileurl: fileurl, localpath: path, IsDir: dir}
}
var DefaultPrefixFn = func() string {
hostname, err := os.Hostname()
if err != nil {
fmt.Fprintln(os.Stderr, "Could not get hostname with os.Hostname()")
hostname = "unknown"
}
var username string
userobj, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, "Could not get user with user.Current()")
username = "unknown"
} else {
username = userobj.Username
}
return fmt.Sprintf("%s@%s", username, hostname)
}