Skip to content

Commit

Permalink
Few changes
Browse files Browse the repository at this point in the history
- Move GetFileWithLogin to pkg/httpClient

- Rename httpClient.GetLogin to httpClient.GetWithLogin

- Remove http client passed as argument, now each script defines its own timeout

- Change fetchVideo.foldersDir

- Fix style
  • Loading branch information
Miguel-Dorta committed Sep 16, 2019
1 parent 4f7f465 commit 14752b3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
8 changes: 4 additions & 4 deletions cmd/fetchImage/fetchImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"flag"
"fmt"
"github.com/Miguel-Dorta/surveillance-cameras/internal"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/utils"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/httpClient"
"golang.org/x/sys/unix"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand All @@ -18,7 +17,6 @@ var (
destinyDir, fileExtension string
printVersion bool
tomorrow time.Time
httpClient = http.Client{Timeout: time.Second}
)

func init() {
Expand All @@ -29,6 +27,8 @@ func init() {
flag.StringVar(&path, "path", "", "Path for saving the images")
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
flag.BoolVar(&printVersion, "V", false, "Print version and exit")

httpClient.Client.Timeout = time.Second
}

func checkFlags() {
Expand Down Expand Up @@ -70,7 +70,7 @@ MainLoop:
requestTime := time.Now()
updateDestinyDir(requestTime)

if err := utils.GetFileWithLogin(url, user, pass, getNewFilePath(path, requestTime), httpClient); err != nil {
if err := httpClient.GetFileWithLogin(url, user, pass, getNewFilePath(path, requestTime)); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error downloading image: %s", err)
}
}
Expand Down
27 changes: 13 additions & 14 deletions cmd/fetchVideo/fetchVideo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import (
"github.com/Miguel-Dorta/surveillance-cameras/pkg/cameras"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/html"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/httpClient"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/utils"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
)

const (
foldersDir = "/tmpfs/sd/"
videoDir = "record000/"
foldersDir = "/sd/"
videoDir = "record000/"
)

var (
url, user, pass, camName, destination string
printVersion bool
logErr = log.New(os.Stderr, "", 0)
httpC = http.Client{}
printVersion bool
logErr = log.New(os.Stderr, "", 0)
)

func init() {
Expand All @@ -35,6 +33,8 @@ func init() {
flag.StringVar(&camName, "camera-name", "", "Sets the camera name/ID")
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
flag.BoolVar(&printVersion, "V", false, "Print version and exit")

httpClient.Client.Timeout = time.Hour
}

func parseFlags() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func main() {
if err != nil {
logErr.Fatalf("cannot create parent directories of file \"%s\": %s", link.Text, err)
}
if err = utils.GetFileWithLogin(url + link.HREF, user, pass, pathToSave, httpC); err != nil {
if err = httpClient.GetFileWithLogin(url+link.HREF, user, pass, pathToSave); err != nil {
logErr.Printf("error saving file in path \"%s\": %s", pathToSave, err)
errFound = true
continue
Expand All @@ -77,7 +77,7 @@ func main() {

func createAndGetSavingPath(filename, camName, destination string) (string, error) {
y, m, d, rest := cameras.GetInfoFromFilenameOWIPCAN45(filename)
parentDirPath := filepath.Join(destination, camName, "20" + y, m, d)
parentDirPath := filepath.Join(destination, camName, "20"+y, m, d)
if err := os.MkdirAll(parentDirPath, 0755); err != nil {
return "", fmt.Errorf("error creating parent directories: %s", err)
}
Expand All @@ -86,19 +86,19 @@ func createAndGetSavingPath(filename, camName, destination string) (string, erro

func getAllVideos(url, user, pass string) ([]html.A, error) {
// Get page
page, err := getPage(url + foldersDir, user, pass)
page, err := getPage(url+foldersDir, user, pass)
if err != nil {
return nil, fmt.Errorf("error getting page from URL \"%s\": %s", url, err)
}

// Get folders from page
aList := html.GetAList(page)
videoList := make([]html.A, 0, len(aList) * 100)
videoList := make([]html.A, 0, len(aList)*100)
for _, a := range aList {
if !cameras.IsValidFolderName(a.Text) {
continue
}
videos, err := getVideoLinks(url + a.HREF + videoDir, user, pass)
videos, err := getVideoLinks(url+a.HREF+videoDir, user, pass)
if err != nil {
return nil, fmt.Errorf("error getting videos from URL \"%s\": %s", a.HREF, err)
}
Expand All @@ -125,7 +125,7 @@ func getVideoLinks(url, user, pass string) ([]html.A, error) {
}

func getPage(url, user, pass string) ([]byte, error) {
resp, err := httpClient.GetLogin(url, user, pass, httpC)
resp, err := httpClient.GetWithLogin(url, user, pass)
if err != nil {
return nil, fmt.Errorf("error getting page from URL \"%s\": %s", url, err)
}
Expand All @@ -138,4 +138,3 @@ func getPage(url, user, pass string) ([]byte, error) {

return data, nil
}

35 changes: 33 additions & 2 deletions pkg/httpClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@ package httpClient

import (
"fmt"
"io"
"net/http"
"os"
)

func GetLogin(url, user, pass string, c http.Client) (*http.Response, error) {
const bufferSize = 128 * 1024

var Client *http.Client

func GetWithLogin(url, user, pass string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %s", err)
}
if user != "" || pass != "" {
req.SetBasicAuth(user, pass)
}
return c.Do(req)
return Client.Do(req)
}


func GetFileWithLogin(url, user, pass, destination string) error {
resp, err := GetWithLogin(url, user, pass)
if err != nil {
return fmt.Errorf("error doing http request to URL \"%s\": %s", url, err)
}
defer resp.Body.Close()

f, err := os.Create(destination)
if err != nil {
return fmt.Errorf("error creating file \"%s\": %s", destination, err)
}
defer f.Close()

if _, err = io.CopyBuffer(f, resp.Body, make([]byte, bufferSize)); err != nil {
return fmt.Errorf("error while saving file \"%s\": %s", destination, err)
}

if err = f.Close(); err != nil {
return fmt.Errorf("error closing file \"%s\": %s", destination, err)
}

return nil
}
26 changes: 0 additions & 26 deletions pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package utils

import (
"fmt"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/httpClient"
"golang.org/x/sys/unix"
"io"
"net/http"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -71,27 +69,3 @@ func Copy(origin, destiny string) error {

return nil
}

func GetFileWithLogin(url, user, pass, destination string, c http.Client) error {
resp, err := httpClient.GetLogin(url, user, pass, c)
if err != nil {
return fmt.Errorf("error doing http request to URL \"%s\": %s", url, err)
}
defer resp.Body.Close()

f, err := os.Create(destination)
if err != nil {
return fmt.Errorf("error creating file \"%s\": %s", destination, err)
}
defer f.Close()

if _, err = io.CopyBuffer(f, resp.Body, copyBuffer); err != nil {
return fmt.Errorf("error while saving file \"%s\": %s", destination, err)
}

if err = f.Close(); err != nil {
return fmt.Errorf("error closing file \"%s\": %s", destination, err)
}

return nil
}

0 comments on commit 14752b3

Please sign in to comment.