Improve documentation

This commit is contained in:
Remi Reuvekamp
2018-06-04 21:54:16 +02:00
parent a67c04b2fa
commit 51d5d00daf

View File

@@ -415,18 +415,25 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
} }
} }
// handleFiles uploads or places all files on the given msg to the MediaServer and
// adds the new URL of the file on the MediaServer onto the given msg.
func (gw *Gateway) handleFiles(msg *config.Message) { func (gw *Gateway) handleFiles(msg *config.Message) {
reg := regexp.MustCompile("[^a-zA-Z0-9]+") reg := regexp.MustCompile("[^a-zA-Z0-9]+")
// if we don't have a attachfield or we don't have a mediaserver configured return
// If we don't have a attachfield or we don't have a mediaserver configured return
if msg.Extra == nil || (gw.Config.General.MediaServerUpload == "" && gw.Config.General.MediaServerPath == "") { if msg.Extra == nil || (gw.Config.General.MediaServerUpload == "" && gw.Config.General.MediaServerPath == "") {
return return
} }
// if we actually have files, start uploading them to the mediaserver // If we don't have files, nothing to upload.
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) == 0 {
return
}
client := &http.Client{ client := &http.Client{
Timeout: time.Second * 5, Timeout: time.Second * 5,
} }
for i, f := range msg.Extra["file"] { for i, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
ext := filepath.Ext(fi.Name) ext := filepath.Ext(fi.Name)
@@ -435,8 +442,9 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
fi.Name = fi.Name + ext fi.Name = fi.Name + ext
sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data)) sha1sum := fmt.Sprintf("%x", sha1.Sum(*fi.Data))
durl := gw.Config.General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name
if gw.Config.General.MediaServerUpload != "" { if gw.Config.General.MediaServerUpload != "" {
// Use MediaServerUpload. Upload using a PUT HTTP request and basicauth.
url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name url := gw.Config.General.MediaServerUpload + "/" + sha1sum + "/" + fi.Name
req, err := http.NewRequest("PUT", url, bytes.NewReader(*fi.Data)) req, err := http.NewRequest("PUT", url, bytes.NewReader(*fi.Data))
@@ -454,6 +462,8 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
continue continue
} }
} else { } else {
// Use MediaServerPath. Place the file on the current filesystem.
dir := gw.Config.General.MediaServerPath + "/" + sha1sum dir := gw.Config.General.MediaServerPath + "/" + sha1sum
err := os.Mkdir(dir, os.ModePerm) err := os.Mkdir(dir, os.ModePerm)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
@@ -471,14 +481,16 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
} }
} }
// Download URL.
durl := gw.Config.General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name
flog.Debugf("mediaserver download URL = %s", durl)
// We uploaded/placed the file successfully. Add the SHA and URL. // We uploaded/placed the file successfully. Add the SHA and URL.
extra := msg.Extra["file"][i].(config.FileInfo) extra := msg.Extra["file"][i].(config.FileInfo)
extra.URL = durl extra.URL = durl
extra.SHA = sha1sum extra.SHA = sha1sum
msg.Extra["file"][i] = extra msg.Extra["file"][i] = extra
flog.Debugf("mediaserver download URL = %s", durl)
}
} }
} }