Skip to content

Commit

Permalink
Downloads now have a really cool progress bar!
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy <sandy@sandyuraz.com>
  • Loading branch information
thecsw committed Feb 12, 2023
1 parent b856c13 commit d52b9d0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 71 deletions.
17 changes: 9 additions & 8 deletions emilia/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func NewGalleryItem(page *yunyun.Page, content *yunyun.Content, wholeLine string
}
}

// GalleryImage takes a gallery item and returns its full path depending
// on the option, so whether it's a full link or a vendored path.
func GalleryImage(item *GalleryItem) yunyun.FullPathFile {
if item.IsExternal {
// If it's vendored, then retrieve a local copy (if doesn't already
Expand Down Expand Up @@ -138,10 +140,10 @@ func galleryVendorItem(item *GalleryItem) yunyun.FullPathFile {
return expectedReturn
}

start := time.Now()
fmt.Printf("Vendoring %s... ", vendoredImagePath)
//start := time.Now()
//fmt.Printf("Vendoring %s... ", vendoredImagePath)

img, err := DownloadImage(string(item.Item))
img, err := DownloadImage(string(item.Item), "vendor", "", string(galleryItemHash(item)))
if err != nil {
fmt.Printf("failed to vendor: %s\n", err.Error())
return fallbackReturn
Expand All @@ -161,15 +163,15 @@ func galleryVendorItem(item *GalleryItem) yunyun.FullPathFile {
return fallbackReturn
}

finish := time.Now()
fmt.Printf("done in %d ms\n", finish.Sub(start).Milliseconds())
//finish := time.Now()
//fmt.Printf("done in %d ms\n", finish.Sub(start).Milliseconds())

// Finally.
return expectedReturn
}

// GalleryItemToImage takes in a gallery item and returns an image object.
func GalleryItemToImage(item *GalleryItem) (image.Image, error) {
func GalleryItemToImage(item *GalleryItem, authority, prefix string) (image.Image, error) {
// If it's a local file, simply open the os file.
if !item.IsExternal {
file := JoinWorkdir(yunyun.JoinRelativePaths(item.Path, item.Item))
Expand All @@ -179,12 +181,11 @@ func GalleryItemToImage(item *GalleryItem) (image.Image, error) {
// Check if the item has been vendored by any chance?
vendorPath := filepath.Join(Config.WorkDir, string(GalleryVendored(item)))
if FileExists(vendorPath) {
fmt.Printf(" (vendored) ")
return OpenImage(vendorPath)
}

// If it's a remote file, then ask Emilia to try and fetch it.
return DownloadImage(string(item.Item))
return DownloadImage(string(item.Item), authority, prefix, string(galleryItemHash(item)))
}

// galleryItemHash returns a hashed name of a gallery item link.
Expand Down
46 changes: 33 additions & 13 deletions emilia/imaging.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package emilia

import (
"bytes"
"fmt"
"image"
"net/http"
"io"
"os"
"path/filepath"

"github.com/disintegration/imaging"
"github.com/k0kubun/go-ansi"
"github.com/pkg/errors"
"github.com/schollz/progressbar/v3"
"github.com/thecsw/haruhi"
)

// OpenImage opens local path image and returns decoded image.
Expand All @@ -23,28 +27,44 @@ func OpenImage(path string) (image.Image, error) {

// DownloadImage attempts to download an image and returns it
// with any fatal errors (if occured).
func DownloadImage(link string) (image.Image, error) {
// Build the request.
req, err := http.NewRequest(http.MethodGet, link, nil)
func DownloadImage(link string, authority, prefix, name string) (image.Image, error) {
resp, cancel, err := haruhi.URL(link).Response()
defer cancel()
if err != nil {
return nil, errors.Wrap(err, "DownloadImage: create request")
return nil, err
}

// Attempt to make the request.
resp, err := vendorClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "DownloadImage: Do request")
}

// If we got not found or server issue, bail.
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return nil, errors.Wrap(err,
fmt.Sprintf("DownloadImage: Bad status: %d", resp.StatusCode))
}
defer resp.Body.Close()

buf := new(bytes.Buffer)
bar := progressbar.NewOptions64(resp.ContentLength,
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(30),
progressbar.OptionSetPredictTime(true),
progressbar.OptionShowCount(),
progressbar.OptionShowElapsedTimeOnFinish(),
progressbar.OptionSetDescription(
fmt.Sprintf("[cyan][%s][reset] %sDownloading %s", authority, prefix, name)),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[magenta]=[reset]",
SaucerHead: "[yellow]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
)
if _, err := io.Copy(io.MultiWriter(buf, bar), resp.Body); err != nil && err != io.EOF {
return nil, errors.Wrap(err, "failed to read the image data")
}

// Attempt to decode.
img, err := imaging.Decode(resp.Body, imaging.AutoOrientation(true))
img, err := imaging.Decode(buf, imaging.AutoOrientation(true))
if err != nil {
return nil, errors.Wrap(err, "DownloadImage: failed to decode")
}
Expand Down
2 changes: 1 addition & 1 deletion emilia/syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func FileExists(path string) bool {
// Mkdir creates a directory and reports fatal errors.
func Mkdir(path string) error {
// Make sure that the vendor directory exists.
err := os.Mkdir(string(path), 0750)
err := os.MkdirAll(string(path), 0750)
// If we couldn't create the vendor directory and it doesn't
// exist, then turn off the vendor option.
if err != nil && !os.IsExist(err) {
Expand Down
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ require github.com/BurntSushi/toml v1.2.1

require (
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
golang.org/x/exp v0.0.0-20230212135524-a684f29349b6 // indirect
golang.org/x/image v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
)

require (
github.com/charmbracelet/lipgloss v0.6.0
github.com/disintegration/imaging v1.6.2
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.8
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/karrick/godirwalk v1.17.0
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.7.0
github.com/schollz/progressbar/v3 v3.13.0
github.com/thecsw/gana v0.0.0-20221119221619-ec55f13e6a5d
github.com/thecsw/haruhi v0.0.0-20230212193023-1d4512254cfd
)
46 changes: 19 additions & 27 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,32 @@ github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTx
github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
Expand All @@ -43,30 +39,26 @@ github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA=
github.com/pkg/profile v1.7.0/go.mod h1:8Uer0jas47ZQMJ7VD+OHknK4YDY07LPUC6dEvqDjvNo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/schollz/progressbar/v3 v3.13.0 h1:9TeeWRcjW2qd05I8Kf9knPkW4vLM/hYoa6z9ABvxje8=
github.com/schollz/progressbar/v3 v3.13.0/go.mod h1:ZBYnSuLAX2LU8P8UiKN/KgF2DY58AJC8yfVYLPC8Ly4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/thecsw/gana v0.0.0-20221119221619-ec55f13e6a5d h1:EwCOe/dtLb8LhlSm1y0B2PKgDrCX2T6Mg8t9PhiGCLM=
github.com/thecsw/gana v0.0.0-20221119221619-ec55f13e6a5d/go.mod h1:peUsRpO0qD0S5GdV+95GLWBNOUq59MuA1l1HRfL65aw=
github.com/thecsw/haruhi v0.0.0-20230212193023-1d4512254cfd h1:lJM29JOH/IkuwIceRIx1GmTcUU0wDEJnN+AfU/U/tRk=
github.com/thecsw/haruhi v0.0.0-20230212193023-1d4512254cfd/go.mod h1:LqtWkO3R58Osjp+YUSljW2JftpM2P2OmOWh4SWR2fSc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ=
golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230212135524-a684f29349b6 h1:Ic9KukPQ7PegFzHckNiMTQXGgEszA7mY2Fn4ZMtnMbw=
golang.org/x/exp v0.0.0-20230212135524-a684f29349b6/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.1.0 h1:r8Oj8ZA2Xy12/b5KZYj3tuv7NG/fBz3TwQVvpJ9l8Rk=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=
Expand All @@ -80,15 +72,18 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand All @@ -97,7 +92,4 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 9 additions & 14 deletions ichika/galleries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"image"
"os"
"path/filepath"
"time"

"github.com/disintegration/imaging"
"github.com/thecsw/darkness/emilia"
"github.com/thecsw/gana"
)

const (
Expand All @@ -26,23 +26,20 @@ func buildGalleryFiles(dryRun bool) {
os.Exit(1)
}
galleryFiles := getGalleryFiles()
for i, galleryFile := range galleryFiles {
fmt.Printf("[%d/%d] ", i+1, len(galleryFiles))
newFile := emilia.GalleryPreview(galleryFile)
if emilia.FileExists(string(newFile)) {
fmt.Printf("%s already exists\n", emilia.FullPathToWorkDirRel(newFile))
continue
}
fmt.Printf("%s... ", emilia.FullPathToWorkDirRel(newFile))

// Mark the processing start time.
start := time.Now()
missingFiles := gana.Filter(func(item *emilia.GalleryItem) bool {
return !emilia.FileExists(string(emilia.GalleryPreview(item)))
}, galleryFiles)

for i, galleryFile := range missingFiles {
newFile := emilia.GalleryPreview(galleryFile)

// Retrieve image contents reader:
// - For local files, it's a reader of the file.
// - For remote files, it's a reader of the response body,
// unless it's vendored, then it's a read of the vendored file.
sourceImage, err := emilia.GalleryItemToImage(galleryFile)
sourceImage, err := emilia.GalleryItemToImage(galleryFile, "preview",
fmt.Sprintf("[%d/%d] ", i+1, len(missingFiles)))
if err != nil {
fmt.Println("gallery item to reader:", err.Error())
continue
Expand All @@ -69,8 +66,6 @@ func buildGalleryFiles(dryRun bool) {
}
file.Close()
}

fmt.Printf("%d ms\n", time.Since(start).Milliseconds())
}
}

Expand Down

0 comments on commit d52b9d0

Please sign in to comment.