Skip to content

Commit

Permalink
TextDeferred function to autogenerate assets from compressed single-t…
Browse files Browse the repository at this point in the history
…exts
  • Loading branch information
bengarrett committed Sep 23, 2024
1 parent 1505d5f commit 963a4b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
8 changes: 7 additions & 1 deletion handler/app/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ func (dir Dirs) Editor(art *models.File, data map[string]interface{}) map[string
if art == nil {
return data
}
d := command.Dirs{
Download: dir.Download,
Preview: dir.Preview,
Thumbnail: dir.Thumbnail,
Extra: dir.Extra,
}
unid := filerecord.UnID(art)
abs := filepath.Join(dir.Download, unid)
data["epochYear"] = epoch
Expand All @@ -365,7 +371,7 @@ func (dir Dirs) Editor(art *models.File, data map[string]interface{}) map[string
data["modMagicNumber"] = simple.MagicAsTitle(abs)
data["modDBModify"] = filerecord.LastModificationDate(art)
data["modStatModify"], data["modStatSizeB"], data["modStatSizeF"] = simple.StatHumanize(abs)
data["modDecompress"] = filerecord.ListContent(art, abs)
data["modDecompress"] = filerecord.ListContent(art, d, abs)
data["modDecompressLoc"] = simple.MkContent(abs)
data["modAssetPreview"] = dir.assets(dir.Preview, unid)
data["modAssetThumbnail"] = dir.assets(dir.Thumbnail, unid)
Expand Down
11 changes: 10 additions & 1 deletion handler/app/internal/filerecord/filerecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/Defacto2/server/handler/app/internal/simple"
"github.com/Defacto2/server/handler/jsdos/msdos"
"github.com/Defacto2/server/handler/readme"
"github.com/Defacto2/server/internal/command"
"github.com/Defacto2/server/internal/postgres/models"
"github.com/Defacto2/server/internal/tags"
"github.com/Defacto2/server/model"
Expand Down Expand Up @@ -481,7 +482,7 @@ func (e *entry) parseMusicID3(path string) bool {

// ListContent returns a list of the files contained in the archive file.
// This is used to generate the HTML for the "Download content" section of the File editor.
func ListContent(art *models.File, src string) template.HTML { //nolint:funlen
func ListContent(art *models.File, dirs command.Dirs, src string) template.HTML { //nolint:funlen
if art == nil {
return ""
}
Expand Down Expand Up @@ -510,6 +511,7 @@ func ListContent(art *models.File, src string) template.HTML { //nolint:funlen
return template.HTML(err.Error())
}
var b strings.Builder
name := ""
walkerFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return filepath.SkipDir
Expand All @@ -527,6 +529,9 @@ func ListContent(art *models.File, src string) template.HTML { //nolint:funlen
return skipEntry
}
entries++
if e.text {
name = d.Name()
}
le := listEntry(e, rel, unid)
b.WriteString(le.HTML(e.bytes, platform, section))
if maxItems := 200; entries > maxItems {
Expand All @@ -539,6 +544,10 @@ func ListContent(art *models.File, src string) template.HTML { //nolint:funlen
if err = filepath.WalkDir(dst, walkerFunc); err != nil {
return template.HTML(err.Error())
}
if entries == 1 && name != "" {
src := filepath.Join(dst, name)
defer func() { _ = dirs.TextDeferred(src, unid) }()
}
b.WriteString(skippedEmpty(zeroByteFiles))
return template.HTML(b.String())
}
Expand Down
1 change: 1 addition & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Dirs struct {
Download string // Download is the directory path for the file downloads.
Preview string // Preview is the directory path for the image previews.
Thumbnail string // Thumbnail is the directory path for the image thumbnails.
Extra string // Extra is the directory path for the extra files.
}

const (
Expand Down
29 changes: 29 additions & 0 deletions internal/command/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,3 +950,32 @@ func OptimizePNG(src string) error {
arg = append(arg, args...) // command line arguments
return RunQuiet(Optipng, arg...)
}

// TextDeferred is used to create a thumbnail and a text file in the extra directory.
// It is intended to be used with the filerecord.ListContent function.
func (dir Dirs) TextDeferred(src, unid string) error {
thumb := false
for _, ext := range ImagesExt() {
src := filepath.Join(dir.Thumbnail, unid+ext)
st, err := os.Stat(src)
if err != nil {
continue
}
if st.Size() > 0 {
thumb = true
break
}
}
if !thumb {
if err := dir.TextImager(nil, src, unid, false); err != nil {
return fmt.Errorf("text deferred %w", err)
}
}
newpath := filepath.Join(dir.Extra, unid+".txt")
if st, err := os.Stat(newpath); err != nil || st.Size() == 0 {
if _, err1 := helper.DuplicateOW(src, newpath); err1 != nil {
return fmt.Errorf("text deferred %w", err1)
}
}
return nil
}
8 changes: 4 additions & 4 deletions view/app/uploader_modal.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@
<div class="input-group mb-3">
<label class="form-label">Operating system</label>
<div class="btn-group w-100" role="group" aria-label="Basic radio toggle button group">
<input type="radio" value="{{tagWindows}}" class="btn-check" name="uploader-intro-operating-system" id="uploader-intro-windows" autocomplete="off" checked>
<input type="radio" value="{{tagWindows}}" class="btn-check" name="uploader-intro-operating-system" id="uploader-intro-windows" autocomplete="off">
<label class="btn btn-outline-primary" for="uploader-intro-windows" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-title="Microsoft&nbsp;Windows">Windows PC</label>
<input type="radio" value="{{tagDOS}}" class="btn-check" name="uploader-intro-operating-system" id="uploader-intro-msdos" autocomplete="off">
<input type="radio" value="{{tagDOS}}" class="btn-check" name="uploader-intro-operating-system" id="uploader-intro-msdos" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="uploader-intro-msdos" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-title="IBM&nbsp;PC or MS-DOS compatible">Dos PC</label>
</div>
Expand Down Expand Up @@ -252,10 +252,10 @@
data-bs-title="A installation app for a release">Installer</label>
</div>
<div class="btn-group w-100 mt-1" role="group" aria-label="Basic radio toggle button group">
<input type="radio" value="{{tagWindows}}" class="btn-check" name="uploader-trainer-operating-system" id="uploader-trainer-windows" autocomplete="off" checked>
<input type="radio" value="{{tagWindows}}" class="btn-check" name="uploader-trainer-operating-system" id="uploader-trainer-windows" autocomplete="off">
<label class="btn btn-outline-primary w-50" for="uploader-trainer-windows" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-title="Microsoft&nbsp;Windows">Windows PC</label>
<input type="radio" value="{{tagDOS}}" class="btn-check" name="uploader-trainer-operating-system" id="uploader-trainer-msdos" autocomplete="off">
<input type="radio" value="{{tagDOS}}" class="btn-check" name="uploader-trainer-operating-system" id="uploader-trainer-msdos" autocomplete="off" checked>
<label class="btn btn-outline-primary w-50" for="uploader-trainer-msdos" data-bs-toggle="tooltip" data-bs-placement="top"
data-bs-title="IBM&nbsp;PC or MS-DOS compatible">Dos PC</label>
</div>
Expand Down

0 comments on commit 963a4b8

Please sign in to comment.