Skip to content

Commit

Permalink
Replaced packr2 with go.rice since packr2 has (#13)
Browse files Browse the repository at this point in the history
bugs that will fail Windows build on Appveyor.
  • Loading branch information
midstar committed Apr 4, 2019
1 parent 6614da2 commit 032ea89
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 56 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ debug*
tmpout
tmpcache
screenshottemp
main-packr.go
a_main-packr.go
packrd
rice-box.go
mediaweb
mediaweb.exe
mediaweb.log
Expand Down
4 changes: 2 additions & 2 deletions main_common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
packr "github.com/gobuffalo/packr/v2"
"github.com/GeertJohan/go.rice"
"github.com/midstar/llog"
)

Expand All @@ -15,7 +15,7 @@ func mainCommon() *WebAPI {
llog.Info("Version: %s", applicationVersion)
llog.Info("Build time: %s", applicationBuildTime)
llog.Info("Git hash: %s", applicationGitHash)
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, s.mediaPath, s.thumbPath,
s.enableThumbCache, s.genThumbsOnStartup, s.autoRotate)
webAPI := CreateWebAPI(s.port, "templates", media, box, s.userName, s.password)
Expand Down
18 changes: 9 additions & 9 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"strings"
"time"

"github.com/GeertJohan/go.rice"
"github.com/disintegration/imaging"
packr "github.com/gobuffalo/packr/v2"
"github.com/midstar/llog"
"github.com/rwcarlsen/goexif/exif"
)
Expand All @@ -23,12 +23,12 @@ var vidExtensions = [...]string{".avi", ".mov", ".vid", ".mkv", ".mp4"}

// Media represents the media including its base path
type Media struct {
mediaPath string // Top level path for media files
thumbPath string // Top level path for thumbnails
enableThumbCache bool // Generate thumbnails
autoRotate bool // Rotate JPEG files when needed
box *packr.Box // For icons
thumbGenInProgress bool // True if thumbnail generation in progress
mediaPath string // Top level path for media files
thumbPath string // Top level path for thumbnails
enableThumbCache bool // Generate thumbnails
autoRotate bool // Rotate JPEG files when needed
box *rice.Box // For icons
thumbGenInProgress bool // True if thumbnail generation in progress
}

// File represents a folder or any other file
Expand All @@ -40,7 +40,7 @@ type File struct {

// createMedia creates a new media. If thumb cache is enabled the path is
// created when needed.
func createMedia(box *packr.Box, mediaPath string, thumbPath string, enableThumbCache, genThumbsOnStartup, autoRotate bool) *Media {
func createMedia(box *rice.Box, mediaPath string, thumbPath string, enableThumbCache, genThumbsOnStartup, autoRotate bool) *Media {
llog.Info("Media path: %s", mediaPath)
if enableThumbCache {
directory := filepath.Dir(thumbPath)
Expand Down Expand Up @@ -482,7 +482,7 @@ func (m *Media) getVideoIcon() (image.Image, error) {
return videoIcon, nil
}
var err error
videoIconBytes, _ := m.box.Find("icon_video.png")
videoIconBytes, _ := m.box.Bytes("icon_video.png")
videoIcon, err = imaging.Decode(bytes.NewReader(videoIconBytes))
if err != nil {
return nil, err
Expand Down
32 changes: 16 additions & 16 deletions media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

packr "github.com/gobuffalo/packr/v2"
rice "github.com/GeertJohan/go.rice"
)

type timerType struct {
Expand All @@ -30,31 +30,31 @@ func LogTime(t *testing.T, whatWasMeasured string) {
}

func TestGetFiles(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)
files, err := media.getFiles("")
assertExpectNoErr(t, "", err)
assertTrue(t, "No files found", len(files) > 5)
}

func TestGetFilesInvalid(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)
files, err := media.getFiles("invalidfolder")
assertExpectErr(t, "invalid path shall give errors", err)
assertTrue(t, "Should not find any files", len(files) == 0)
}

func TestGetFilesHacker(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)
files, err := media.getFiles("../..")
assertExpectErr(t, "hacker path shall give errors", err)
assertTrue(t, "Should not find any files", len(files) == 0)
}

func TestIsRotationNeeded(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)

rotationNeeded := media.isRotationNeeded("exif_rotate/180deg.jpg")
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestRotateAndWrite(t *testing.T) {
outFileName := "tmpout/TestRotateAndWrite/jpeg_rotated_fixed.jpg"
os.MkdirAll("tmpout/TestRotateAndWrite", os.ModePerm) // If already exist no problem
os.Remove(outFileName)
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)
outFile, err := os.Create(outFileName)
assertExpectNoErr(t, "unable to create out", err)
Expand Down Expand Up @@ -134,7 +134,7 @@ func tEXIFThumbnail(t *testing.T, media *Media, filename string) {

func TestWriteEXIFThumbnail(t *testing.T) {
os.MkdirAll("tmpout/TestWriteEXIFThumbnail", os.ModePerm) // If already exist no problem
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", ".", true, false, true)

tEXIFThumbnail(t, media, "normal.jpg")
Expand All @@ -159,7 +159,7 @@ func TestWriteEXIFThumbnail(t *testing.T) {

func TestFullPath(t *testing.T) {
// Root path
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, ".", ".", true, false, true)
p, err := media.getFullMediaPath("afile.jpg")
assertExpectNoErr(t, "unable to get valid full path", err)
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestFullPath(t *testing.T) {
}

func TestThumbnailPath(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "/c/mediapath", "/d/thumbpath", true, false, true)

thumbPath, err := media.thumbnailPath("myimage.jpg")
Expand Down Expand Up @@ -224,7 +224,7 @@ func tGenerateImageThumbnail(t *testing.T, media *Media, inFileName, outFileName
func TestGenerateImageThumbnail(t *testing.T) {
os.MkdirAll("tmpout/TestGenerateImageThumbnail", os.ModePerm) // If already exist no problem

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "", "", true, false, true)

tGenerateImageThumbnail(t, media, "testmedia/jpeg.jpg", "tmpout/TestGenerateImageThumbnail/jpeg_thumbnail.jpg")
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestWriteThumbnail(t *testing.T) {
os.RemoveAll("tmpout/TestWriteThumbnail")
os.MkdirAll("tmpout/TestWriteThumbnail", os.ModePerm)

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", "tmpcache/TestWriteThumbnail", true, false, true)

// JPEG with embedded EXIF
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestVideoThumbnailSupport(t *testing.T) {
ffmpegCmd = origCmd
}()

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "", "", true, false, true)

t.Logf("ffmpeg supported: %v", media.videoThumbnailSupport())
Expand Down Expand Up @@ -333,7 +333,7 @@ func tGenerateVideoThumbnail(t *testing.T, media *Media, inFileName, outFileName
}

func TestGenerateVideoThumbnail(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "", "", true, false, true)
if !media.videoThumbnailSupport() {
t.Skip("ffmpeg not installed skipping test")
Expand All @@ -359,7 +359,7 @@ func TestGenerateThumbnails(t *testing.T) {
os.RemoveAll(cache)
os.MkdirAll(cache, os.ModePerm)

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", cache, true, false, true)
stat := media.generateThumbnails("")
assertEqualsInt(t, "", 1, stat.NbrOfFolders)
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestGenerateAllThumbnails(t *testing.T) {
os.RemoveAll(cache)
os.MkdirAll(cache, os.ModePerm)

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", cache, true, true, true)

for i := 0; i < 300; i++ {
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestGenerateNoThumbnails(t *testing.T) {
os.RemoveAll(cache)
os.MkdirAll(cache, os.ModePerm)

box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", cache, true, false, true)

assertFalse(t, "", media.thumbGenInProgress)
Expand Down
13 changes: 3 additions & 10 deletions scripts/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ for /f %%i in ('git rev-parse HEAD') do set GITHASH=%%i
echo git hash: %GITHASH%
echo building / installing
cd %GOPATH%\src\github.com\midstar\mediaweb
set PACKRCMD=packr2
echo %PACKRCMD%
%PACKRCMD%
REM There is a bug in packr that creates absolute paths in main-packr.go on some
REM build machines (uncertain why). Replace main-packr.go with a new file
echo Replacing main-packr.go due to a bug in packr library
echo // +build !skippackr > main-packr.go
echo. > main-packr.go
echo package main >> main-packr.go
echo import _ "github.com/midstar/mediaweb/packrd" >> main-packr.go
set RICECMD=rice embed-go
echo %RICECMD%
%RICECMD%
set INSTALLCMD=go build -ldflags="-X 'main.applicationBuildTime=%DATE% %TIME%' -X main.applicationVersion=%VERSION% -X main.applicationGitHash=%GITHASH%" github.com/midstar/mediaweb
echo %INSTALLCMD%
%INSTALLCMD%
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ echo date time: $DATETIME

echo building / installing
cd $GOPATH/src/github.com/midstar/mediaweb
packr2
rice embed-go
go build -ldflags="-X 'main.applicationBuildTime=$DATETIME' -X main.applicationVersion=$1 -X main.applicationGitHash=$GITHASH" github.com/midstar/mediaweb

4 changes: 2 additions & 2 deletions scripts/install_deps.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ go get github.com/midstar/llog
go get github.com/midstar/gocfg
go get github.com/disintegration/imaging
go get github.com/rwcarlsen/goexif/exif
go get -u github.com/gobuffalo/packr/v2/...
go get -u github.com/gobuffalo/packr/v2/packr2
go get github.com/GeertJohan/go.rice
go get github.com/GeertJohan/go.rice/rice
go get github.com/kardianos/service
4 changes: 2 additions & 2 deletions scripts/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ go get github.com/midstar/llog
go get github.com/midstar/gocfg
go get github.com/disintegration/imaging
go get github.com/rwcarlsen/goexif/exif
go get -u github.com/gobuffalo/packr/v2/...
go get -u github.com/gobuffalo/packr/v2/packr2
go get github.com/GeertJohan/go.rice
go get github.com/GeertJohan/go.rice/rice
16 changes: 8 additions & 8 deletions webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"strings"

packr "github.com/gobuffalo/packr/v2"
"github.com/GeertJohan/go.rice"
"github.com/midstar/llog"
)

Expand All @@ -18,13 +18,13 @@ type WebAPI struct {
server *http.Server
templatePath string // Path to the templates
media *Media
box *packr.Box
box *rice.Box
userName string // User name ("" means no authentication)
password string // Password
}

// CreateWebAPI creates a new Web API instance
func CreateWebAPI(port int, templatePath string, media *Media, box *packr.Box, userName, password string) *WebAPI {
func CreateWebAPI(port int, templatePath string, media *Media, box *rice.Box, userName, password string) *WebAPI {
portStr := fmt.Sprintf(":%d", port)
server := &http.Server{Addr: portStr}
webAPI := &WebAPI{
Expand Down Expand Up @@ -105,7 +105,7 @@ func (wa *WebAPI) serveHTTPStatic(w http.ResponseWriter, r *http.Request) {
// Default is index page
fileName = "index.html"
}
bytes, err := wa.box.Find(fileName)
bytes, err := wa.box.Bytes(fileName)
if err != nil || len(bytes) == 0 {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Unable to find: %s!", fileName)
Expand Down Expand Up @@ -168,20 +168,20 @@ func (wa *WebAPI) serveHTTPThumbnail(w http.ResponseWriter, r *http.Request) {
if err == nil {
w.Header().Set("Content-Type", "image/jpeg")
} else {
// No thumbnail. Use the default
// No thumbnail. Use the default
w.Header().Set("Content-Type", "image/png")
fileType := wa.media.getFileType(relativePath)
if fileType == "image" {
iconImage, _ := wa.box.Find("icon_image.png")
iconImage, _ := wa.box.Bytes("icon_image.png")
w.Write(iconImage)
//http.ServeFile(w, r, wa.templatePath+"/icon_image.png")
} else if fileType == "video" {
iconVideo, _ := wa.box.Find("icon_video.png")
iconVideo, _ := wa.box.Bytes("icon_video.png")
w.Write(iconVideo)
//http.ServeFile(w, r, wa.templatePath+"/icon_video.png")
} else {
// Folder
iconFolder, _ := wa.box.Find("icon_folder.png")
iconFolder, _ := wa.box.Bytes("icon_folder.png")
w.Write(iconFolder)
//http.ServeFile(w, r, wa.templatePath+"/icon_folder.png")
}
Expand Down
6 changes: 3 additions & 3 deletions webapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
"time"

packr "github.com/gobuffalo/packr/v2"
rice "github.com/GeertJohan/go.rice"
)

var baseURL = "http://localhost:9834"
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestGetThumbnail(t *testing.T) {
}

func TestGetThumbnailNoCache(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", "", false, false, true)
webAPI := CreateWebAPI(9834, "templates", media, box, "", "")
webAPI.Start()
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestInvalidPath(t *testing.T) {
}

func TestAuthentication(t *testing.T) {
box := packr.New("templates", "./templates")
box := rice.MustFindBox("templates")
media := createMedia(box, "testmedia", "", true, false, true)
webAPI := CreateWebAPI(9834, "templates", media, box, "myuser", "mypass")
webAPI.Start()
Expand Down

0 comments on commit 032ea89

Please sign in to comment.