moved some stuff around, added Stream struct/class
This commit is contained in:
parent
914210f644
commit
bf1f49302e
3 changed files with 112 additions and 90 deletions
96
main.go
96
main.go
|
@ -1,13 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
"time"
|
||||
|
@ -15,36 +11,6 @@ import (
|
|||
"gocv.io/x/gocv"
|
||||
)
|
||||
|
||||
// StreamURL Unexported
|
||||
type StreamURL struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
// Stream Unexported
|
||||
type Stream struct {
|
||||
URL string
|
||||
base64 string
|
||||
streamDirPath string
|
||||
previousInstantPath string
|
||||
currentInstantPath string
|
||||
nextInstantPath string
|
||||
}
|
||||
|
||||
// NewStream Unexported
|
||||
func NewStream(URL string) Stream {
|
||||
base64 := URLToBase64(URL)
|
||||
streamDirPath := filepath.Join(".", "streams", base64)
|
||||
return Stream{
|
||||
URL: URL,
|
||||
base64: base64,
|
||||
streamDirPath: streamDirPath,
|
||||
// Maybe make these three functions ?
|
||||
previousInstantPath: filepath.Join(streamDirPath, "previous.jpg"),
|
||||
currentInstantPath: filepath.Join(streamDirPath, "current.jpg"),
|
||||
nextInstantPath: filepath.Join(streamDirPath, "next.jpg"),
|
||||
}
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
if r.FormValue("URL") == "" {
|
||||
indexTemplate, err := template.ParseFiles(filepath.Join("templates", "index.html"))
|
||||
|
@ -54,7 +20,7 @@ func index(w http.ResponseWriter, r *http.Request) {
|
|||
indexTemplate.Execute(w, nil)
|
||||
return
|
||||
}
|
||||
streamURL := StreamURL{
|
||||
streamURL := Stream{
|
||||
URL: r.FormValue("URL"),
|
||||
}
|
||||
streamTemplate, err := template.ParseFiles(filepath.Join("templates", "stream.html"))
|
||||
|
@ -73,15 +39,13 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
URL := r.FormValue("URL")
|
||||
|
||||
img := getStreamInstant(URL)
|
||||
stream := NewStream(URL)
|
||||
|
||||
go saveStreamInstant(URL, img)
|
||||
img := stream.GetStreamInstant(URL)
|
||||
|
||||
streamDir := URLToBase64(URL)
|
||||
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
||||
previousStreamInstantPath := filepath.Join(streamStoreDir, "previous.jpg")
|
||||
go stream.SaveStreamInstant(URL, img)
|
||||
|
||||
if fileExists(previousStreamInstantPath) {
|
||||
if FileExists(stream.GetPreviousInstantPath()) {
|
||||
newMat := gocv.NewMat()
|
||||
|
||||
mat, err := gocv.IMDecode(img, gocv.IMReadColor)
|
||||
|
@ -92,7 +56,7 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
|||
gocv.CvtColor(mat, &newMat, gocv.ColorBGRToGray)
|
||||
gocv.GaussianBlur(newMat, &newMat, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
|
||||
previous := gocv.IMRead(previousStreamInstantPath, gocv.IMReadColor)
|
||||
previous := gocv.IMRead(stream.GetPreviousInstantPath(), gocv.IMReadColor)
|
||||
|
||||
gocv.CvtColor(previous, &previous, gocv.ColorBGRToGray)
|
||||
gocv.GaussianBlur(previous, &previous, image.Point{X: 21, Y: 21}, 0, 0, gocv.BorderReflect)
|
||||
|
@ -107,43 +71,6 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
|||
w.Write(img)
|
||||
}
|
||||
|
||||
func getStreamInstant(URL string) []byte {
|
||||
resp, err := http.Get(URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
img, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
return img
|
||||
}
|
||||
|
||||
func saveStreamInstant(URL string, img []byte) {
|
||||
streamDir := URLToBase64(URL)
|
||||
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
||||
os.MkdirAll(streamStoreDir, os.ModePerm)
|
||||
|
||||
currentStreamInstantPath := filepath.Join(streamStoreDir, "current.jpg")
|
||||
swap := fileExists(streamStoreDir)
|
||||
|
||||
if swap {
|
||||
currentStreamInstantPath = filepath.Join(streamStoreDir, "next.jpg")
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(currentStreamInstantPath, img, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal("Can't write latest stream instant.")
|
||||
}
|
||||
|
||||
if swap {
|
||||
previousStreamInstantPath := filepath.Join(streamStoreDir, "previous.jpg")
|
||||
nextStreamInstantPath := currentStreamInstantPath
|
||||
currentStreamInstantPath = filepath.Join(streamStoreDir, "current.jpg")
|
||||
|
||||
err = os.Rename(currentStreamInstantPath, previousStreamInstantPath)
|
||||
err = os.Rename(nextStreamInstantPath, currentStreamInstantPath)
|
||||
}
|
||||
}
|
||||
|
||||
func listenAndServe() {
|
||||
staticFileServer := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
|
||||
|
@ -164,14 +91,3 @@ func main() {
|
|||
go listenAndServe()
|
||||
backend()
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func URLToBase64(URL string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(URL))
|
||||
return base64.URLEncoding.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
|
86
stream.go
Executable file
86
stream.go
Executable file
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Stream Unexported
|
||||
type Stream struct {
|
||||
URL string
|
||||
base64 string
|
||||
streamDirPath string
|
||||
}
|
||||
|
||||
// NewStream creates a new Stream Object
|
||||
func NewStream(URL string) Stream {
|
||||
base64 := URLToBase64(URL)
|
||||
streamDirPath := filepath.Join(".", "streams", base64)
|
||||
return Stream{
|
||||
URL: URL,
|
||||
base64: base64,
|
||||
streamDirPath: streamDirPath,
|
||||
}
|
||||
}
|
||||
|
||||
// GetStreamInstant http.Get(URL) and returns the response
|
||||
func (s Stream) GetStreamInstant(URL string) []byte {
|
||||
resp, err := http.Get(URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
img, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
return img
|
||||
}
|
||||
|
||||
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
||||
func (s Stream) SaveStreamInstant(URL string, img []byte) {
|
||||
streamDir := URLToBase64(URL)
|
||||
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
||||
os.MkdirAll(streamStoreDir, os.ModePerm)
|
||||
|
||||
currentStreamInstantPath := filepath.Join(streamStoreDir, "current.jpg")
|
||||
swap := FileExists(streamStoreDir)
|
||||
|
||||
if swap {
|
||||
currentStreamInstantPath = filepath.Join(streamStoreDir, "next.jpg")
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(currentStreamInstantPath, img, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal("Can't write latest stream instant.")
|
||||
}
|
||||
|
||||
if swap {
|
||||
previousStreamInstantPath := filepath.Join(streamStoreDir, "previous.jpg")
|
||||
nextStreamInstantPath := currentStreamInstantPath
|
||||
currentStreamInstantPath = filepath.Join(streamStoreDir, "current.jpg")
|
||||
|
||||
err = os.Rename(currentStreamInstantPath, previousStreamInstantPath)
|
||||
err = os.Rename(nextStreamInstantPath, currentStreamInstantPath)
|
||||
}
|
||||
}
|
||||
|
||||
// GetStreamDirPath returns filepath.Join(".", "streams")
|
||||
func (s Stream) GetStreamDirPath() string {
|
||||
return filepath.Join(".", "streams")
|
||||
}
|
||||
|
||||
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.base64, "previous.jpg")
|
||||
func (s Stream) GetPreviousInstantPath() string {
|
||||
return filepath.Join(s.GetStreamDirPath(), s.base64, "previous.jpg")
|
||||
}
|
||||
|
||||
// GetCurrentInstantPath returns filepath.Join(s.GetStreamDirPath(), s.base64, "current.jpg")
|
||||
func (s Stream) GetCurrentInstantPath() string {
|
||||
return filepath.Join(s.GetStreamDirPath(), s.base64, "current.jpg")
|
||||
}
|
||||
|
||||
// GetLastInstantPath returns filepath.Join(s.GetStreamDirPath(), s.base64, "last.jpg")
|
||||
func (s Stream) GetLastInstantPath() string {
|
||||
return filepath.Join(s.GetStreamDirPath(), s.base64, "last.jpg")
|
||||
}
|
20
util.go
Executable file
20
util.go
Executable file
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"os"
|
||||
)
|
||||
|
||||
// URLToBase64 returns the base64 encoding of the URL parameter
|
||||
func URLToBase64(URL string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(URL))
|
||||
return base64.URLEncoding.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// FileExists returns true if path exists
|
||||
func FileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
Loading…
Add table
Reference in a new issue