Ocean/WebContent/Send.go

51 lines
1.8 KiB
Go
Raw Permalink Normal View History

package WebContent
import (
"bytes"
"errors"
"fmt"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/MimeTypes"
"net/http"
)
2015-06-17 15:44:52 +00:00
// Send any web content file to the client.
func SendContent(response http.ResponseWriter, path string) (err error) {
2015-06-17 15:44:52 +00:00
// Read the bytes:
content, contError := GetContent(path)
if contError != nil {
err = errors.New("Was not able to read the needed content: " + contError.Error())
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameREAD, `Was not able to read the needed content.`, contError.Error(), path)
return
}
2015-06-17 15:44:52 +00:00
// Determine some meta data:
contentLength := len(content)
contentType, typeError := MimeTypes.DetectType(path)
if typeError != nil {
err = errors.New("Was not able to detect the MIME type: " + typeError.Error())
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameANALYSIS, `Was not able to detect the MIME type for the file.`, path, typeError.Error())
return
}
2015-06-17 15:44:52 +00:00
// Send the meta data to the client:
response.Header().Add("Content-Length", fmt.Sprintf("%d", contentLength))
response.Header().Add("Content-Type", contentType.MimeType)
response.WriteHeader(http.StatusOK)
2015-06-17 15:44:52 +00:00
// Write the binary data to the client:
buffer := bytes.NewBuffer(content)
_, writeError := buffer.WriteTo(response)
if writeError != nil {
err = errors.New("Was not able to write the data to the net: " + writeError.Error())
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameWRITE, `Was not able to write the file to the browser.`, path, writeError.Error())
return
}
return
}