mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-24 00:55:41 +00:00
general: cleanup
fetch: adding logic to fetch a remote file. hopefully to have a form, letting you point to an url, to insert and use :-)
This commit is contained in:
parent
37e933a905
commit
904b760b11
1 changed files with 67 additions and 12 deletions
79
imgsrv.go
79
imgsrv.go
|
@ -22,9 +22,11 @@ import (
|
||||||
//"errors"
|
//"errors"
|
||||||
"mime"
|
"mime"
|
||||||
|
|
||||||
|
"crypto/tls"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"hash/adler32"
|
"hash/adler32"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,6 +35,7 @@ var (
|
||||||
|
|
||||||
DefaultRunAsServer = false
|
DefaultRunAsServer = false
|
||||||
RunAsServer = DefaultRunAsServer
|
RunAsServer = DefaultRunAsServer
|
||||||
|
|
||||||
DefaultServerIP = "0.0.0.0"
|
DefaultServerIP = "0.0.0.0"
|
||||||
ServerIP = DefaultServerIP
|
ServerIP = DefaultServerIP
|
||||||
DefaultServerPort = "7777"
|
DefaultServerPort = "7777"
|
||||||
|
@ -52,8 +55,8 @@ var (
|
||||||
|
|
||||||
DefaultRemoteHost = ""
|
DefaultRemoteHost = ""
|
||||||
RemoteHost = DefaultRemoteHost
|
RemoteHost = DefaultRemoteHost
|
||||||
GetFile = ""
|
|
||||||
PutFile = ""
|
PutFile = ""
|
||||||
|
FetchUrl = ""
|
||||||
FileKeywords = ""
|
FileKeywords = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -129,6 +132,50 @@ func getFileRandom() (this_file File, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func fetchFileFromURL(url string) (filename string, err error) {
|
||||||
|
var t time.Time
|
||||||
|
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
|
||||||
|
}
|
||||||
|
client := &http.Client{
|
||||||
|
//CheckRedirect: redirectPolicyFunc,
|
||||||
|
Transport: tr,
|
||||||
|
}
|
||||||
|
resp, err := client.Get(url)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if (err != nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mtime := resp.Header.Get("last-modified")
|
||||||
|
if (len(mtime) > 0) {
|
||||||
|
t, err = time.Parse(http.TimeFormat, mtime)
|
||||||
|
if (err != nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t = time.Now()
|
||||||
|
}
|
||||||
|
_, url_filename := filepath.Split(url)
|
||||||
|
|
||||||
|
log.Println(t)
|
||||||
|
log.Println(url_filename)
|
||||||
|
log.Println(resp)
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if (err != nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
|
||||||
|
if (err != nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
filename = filepath.Join(os.TempDir(), url_filename)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/* return a <a href/> for a given filename
|
/* return a <a href/> for a given filename
|
||||||
and root is the relavtive base of the explicit link.
|
and root is the relavtive base of the explicit link.
|
||||||
*/
|
*/
|
||||||
|
@ -589,14 +636,15 @@ func init() {
|
||||||
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
||||||
|
|
||||||
/* Client-side */
|
/* Client-side */
|
||||||
|
flag.StringVar(&FetchUrl,
|
||||||
|
"fetch",
|
||||||
|
FetchUrl,
|
||||||
|
"Just fetch the file from this url")
|
||||||
|
|
||||||
flag.StringVar(&RemoteHost,
|
flag.StringVar(&RemoteHost,
|
||||||
"remotehost",
|
"remotehost",
|
||||||
RemoteHost,
|
RemoteHost,
|
||||||
"Remote host to get/put files on ('remotehost' in the config)")
|
"Remote host to get/put files on ('remotehost' in the config)")
|
||||||
flag.StringVar(&GetFile,
|
|
||||||
"get",
|
|
||||||
GetFile,
|
|
||||||
"Fetch file on remote server (needs -remotehost)")
|
|
||||||
flag.StringVar(&PutFile,
|
flag.StringVar(&PutFile,
|
||||||
"put",
|
"put",
|
||||||
PutFile,
|
PutFile,
|
||||||
|
@ -666,14 +714,21 @@ func main() {
|
||||||
// to override variables
|
// to override variables
|
||||||
loadConfiguration(ConfigFile)
|
loadConfiguration(ConfigFile)
|
||||||
|
|
||||||
if (RunAsServer) {
|
if (len(FetchUrl) > 0) {
|
||||||
runServer(ServerIP,ServerPort)
|
file, err := fetchFileFromURL(FetchUrl)
|
||||||
|
if (err != nil) {
|
||||||
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
// XXX
|
||||||
|
log.Println(file)
|
||||||
|
} else if (RunAsServer) {
|
||||||
log.Printf("%s", ServerIP)
|
log.Printf("%s", ServerIP)
|
||||||
log.Printf("%d", len(GetFile))
|
runServer(ServerIP,ServerPort)
|
||||||
|
} else {
|
||||||
//log.Printf("Good Morning!")
|
if (len(PutFile) == 0 && len(flag.Args()) == 0) {
|
||||||
//log.Printf("%x", getMd5FromString("Good Morning!"))
|
log.Println("Please provide files to be uploaded!")
|
||||||
|
}
|
||||||
|
log.Printf("%d", len(PutFile))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue