Skip to content

Commit

Permalink
feat: added thumbnail generation logi and wrote test for Fetch function
Browse files Browse the repository at this point in the history
  • Loading branch information
abskrj committed Jan 3, 2023
1 parent 2833da8 commit 75a9812
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
6 changes: 5 additions & 1 deletion service/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ func generateMPD(outputPath string) {

o, err := cmd.CombinedOutput()

utils.DeleteFiles(filePaths)
if err != nil {
log.Println(err)
}

err = utils.DeleteFiles(filePaths)

if err != nil {
log.Println(err)
Expand Down
4 changes: 4 additions & 0 deletions utils/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"zestream-server/constants"
)

/*
Fetch downlods a file to downloads folder from the given url,
and names it as given fileName
*/
func Fetch(url string, fileName string) error {
newFileName, err := GetDownloadFilePathName(fileName)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions utils/fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils_test

import (
"testing"
"zestream-server/utils"
)

func TestFetch(t *testing.T) {
const fileURL = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/WeAreGoingOnBullrun.mp4"
const fileName = "WeAreGoingOnBullrun.mp4"

// Download the file locally
err := utils.Fetch(fileURL, fileName)

if err != nil {
t.Fatal(err)
}

pathName, err := utils.GetDownloadFilePathName(fileName)

if err != nil {
t.Fatal(err)
}

isFileDownloaded := utils.IsFileValid(pathName)

if isFileDownloaded == false {
t.Error("want true, got", isFileDownloaded)
}

t.Error(isFileDownloaded, pathName)

utils.DeleteFiles(pathName)
}
29 changes: 15 additions & 14 deletions utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"bytes"
"errors"
"log"
"os"
"os/exec"
"path"
Expand All @@ -12,6 +11,10 @@ import (
"zestream-server/constants"
)

/*
GetDownloadFilePathName returns the absolute path of the file present in downloads folder
takes fileName as argument
*/
func GetDownloadFilePathName(fileName string) (string, error) {
cwd, err := os.Getwd()

Expand All @@ -32,6 +35,10 @@ func GetDownloadFilePathName(fileName string) (string, error) {
return newPath, nil
}

/*
GetOutputFilePathName returns the absolute path of the file present in downloads folder
takes fileName as argument
*/
func GetOutputFilePathName(fileName string, postfix string) (string, error) {
cwd, err := os.Getwd()

Expand All @@ -55,6 +62,7 @@ func GetOutputFilePathName(fileName string, postfix string) (string, error) {
return newPath, nil
}

// createDirPath creates a directory at the given path
func createDirPath(pathName string) error {

err := os.MkdirAll(pathName, os.ModePerm)
Expand All @@ -66,26 +74,20 @@ func createDirPath(pathName string) error {
return nil
}

// RemoveExtensionFromFile returns the fileName without the extension, if fileName doesn't end with ext, it returns the fileName
func RemoveExtensionFromFile(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}

// IsFileValid checks if the file is present on that path, returns true, if file is there else false
func IsFileValid(filePath string) bool {
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
return false
}
return true
}

func WrapStringInQuotes(str string) string {
var buff bytes.Buffer

buff.WriteString(str)
buff.WriteString(" ")

return buff.String()
}

// StringToArgsGenerator takes a map of arguments, and it returns the command line form of arguments
func StringToArgsGenerator(args map[string]string) string {
var argsStr bytes.Buffer

Expand All @@ -102,10 +104,9 @@ func StringToArgsGenerator(args map[string]string) string {
return argsStr.String()
}

func DeleteFiles(filePaths string) {
// DeleteFiles deletes the file/s given in the filePaths
func DeleteFiles(filePaths string) error {
_, err := exec.Command("rm", strings.Split(filePaths, " ")...).Output()

if err != nil {
log.Println(err)
}
return err
}
15 changes: 15 additions & 0 deletions utils/helper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package utils

import (
"bytes"
"math/rand"
"strconv"
"time"
)

/*
VideoIDGen returns an unique videoID and appends the fileExtension to it,
it takes the fileExtensionas parameter
*/
func VideoIDGen(fileExtension string) string {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
Expand All @@ -16,3 +21,13 @@ func VideoIDGen(fileExtension string) string {
// VideoID = (8-digit random number) + (file Name)
return strconv.Itoa(randomNumber) + fileExtension
}

// WrapStringInQuotes returns the string wrapped in quotes
func WrapStringInQuotes(str string) string {
var buff bytes.Buffer

buff.WriteString(str)
buff.WriteString(" ")

return buff.String()
}

0 comments on commit 75a9812

Please sign in to comment.