Skip to content

Commit

Permalink
lint fixes.
Browse files Browse the repository at this point in the history
unit test fixes.
  • Loading branch information
bengarrett committed Aug 14, 2024
1 parent 1b636bd commit dd0b580
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 117 deletions.
6 changes: 3 additions & 3 deletions assets/js/editor-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { clipValue, getElmById } from "./helper.mjs";
const dataEditors = document.getElementsByName("artifact-editor-dataeditor");
if (dataEditors.length > 0) {
for (let i = 0; i < dataEditors.length; i++) {
dataEditors[i].addEventListener("click", (evt) => {
dataEditors[i].addEventListener("click", () => {
assetModal.hide();
emulateModal.hide();
artifactModal.show();
Expand All @@ -35,7 +35,7 @@ import { clipValue, getElmById } from "./helper.mjs";
const fileEditors = document.getElementsByName("artifact-editor-fileeditor");
if (fileEditors.length > 0) {
for (let i = 0; i < fileEditors.length; i++) {
fileEditors[i].addEventListener("click", (evt) => {
fileEditors[i].addEventListener("click", () => {
artifactModal.hide();
emulateModal.hide();
assetModal.show();
Expand All @@ -45,7 +45,7 @@ import { clipValue, getElmById } from "./helper.mjs";
const emuEditors = document.getElementsByName("artifact-editor-emueditor");
if (emuEditors.length > 0) {
for (let i = 0; i < emuEditors.length; i++) {
emuEditors[i].addEventListener("click", (evt) => {
emuEditors[i].addEventListener("click", () => {
artifactModal.hide();
assetModal.hide();
emulateModal.show();
Expand Down
2 changes: 1 addition & 1 deletion handler/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestNames(t *testing.T) {
t.Parallel()

x := app.Names()
assert.Equal(t, "public/js/editor-artifact.min.js", x[0])
assert.Equal(t, "public/css/bootstrap.min.css", x[0])
}

func TestFontRefs(t *testing.T) {
Expand Down
66 changes: 37 additions & 29 deletions handler/app/internal/mf/mf.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ const (
textamiga = "textamiga"
arrowLink template.HTML = `<svg class="bi" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#arrow-right"></use></svg>`
br = "<br>"
br = "<br>"
bat = ".bat"
cmd = ".cmd"
com = ".com"
exe = ".exe"
ini = ".ini"
)

// ListEntry is a struct for the directory item that is used to generate the HTML.
Expand All @@ -57,6 +62,7 @@ type ListEntry struct {

// HTML returns the HTML for an file item in the "Download content" section of the File editor.
func (m ListEntry) HTML(bytes int64, platform string) string {
const blank = `<div class="col col-1"></div>`
name := url.QueryEscape(m.RelativeName)
ext := strings.ToLower(filepath.Ext(name))
titlename := m.RelativeName
Expand All @@ -75,39 +81,39 @@ func (m ListEntry) HTML(bytes int64, platform string) string {
`hx-target="#artifact-editor-comp-feedback" hx-patch="/editor/preview/copy/%s/%s">`, m.UniqueID, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
case m.Texts && (ext == ".bat" || ext == ".cmd" || ext == ".ini"):
htm += `<div class="col col-1"></div>`
case m.Texts && (ext == bat || ext == cmd || ext == ini):
htm += blank
case m.Texts:
htm += `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" `+
`hx-target="#artifact-editor-comp-feedback" hx-patch="/editor/readme/preview/%s/%s">`, m.UniqueID, name) +
`<svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#images"></use></svg></a></div>`
default:
htm += `<div class="col col-1"></div>`
htm += blank
}
switch {
case m.Texts && (ext == ".bat" || ext == ".cmd" || ext == ".ini"):
htm += `<div class="col col-1"></div>`
case m.Texts && (ext == bat || ext == cmd || ext == ini):
htm += blank
case m.Texts:
htm += `<div class="col col-1 text-end">` +
fmt.Sprintf(`<a class="icon-link align-text-bottom" `+
`hx-target="#artifact-editor-comp-feedback" hx-patch="/editor/readme/copy/%s/%s">`, m.UniqueID, name) +
`<svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#file-text"></use></svg></a></div>`
case m.Programs && ext == ".exe", m.Programs && ext == ".com":
case m.Programs && ext == exe, m.Programs && ext == com:
htm += `<div class="col col-1 text-end"><svg width="16" height="16" fill="currentColor" aria-hidden="true">` +
`<use xlink:href="/svg/bootstrap-icons.svg#terminal-plus"></use></svg></div>`
default:
htm += `<div class="col col-1"></div>`
htm += blank
}
htm += fmt.Sprintf(`<div><small data-bs-toggle="tooltip" data-bs-title="%d bytes">%s</small>`, bytes, m.Filesize)
switch {
case m.Texts && (ext == ".bat" || ext == ".cmd"):
case m.Texts && (ext == bat || ext == cmd):
htm += fmt.Sprintf(` <small class="">%s</small></div>`, "command script")
case m.Texts && (ext == ".ini"):
case m.Texts && (ext == ini):
htm += fmt.Sprintf(` <small class="">%s</small></div>`, "configuration textfile")
case m.Programs || ext == ".com":
case m.Programs || ext == com:
htm = progr(m.Executable, ext, htm, bytes)
case m.MusicConfig != "":
htm += fmt.Sprintf(` <small class="">%s</small></div>`, m.MusicConfig)
Expand All @@ -123,20 +129,16 @@ func (m ListEntry) HTML(bytes int64, platform string) string {
func progr(exec magicnumber.Windows, ext, htm string, bytes int64) string {
const epochYear = 1980
const x8086 = 64 * 1024
s := ""
fmt.Printf("exec: %+v %+v %s\n", exec.PE, exec.NE, ext)
dosProg := (ext == exe || ext == com)
var s string
switch {
case (ext == ".exe" || ext == ".com") && exec.PE != magicnumber.UnknownPE:
s = fmt.Sprintf("%s executable", exec)
case (ext == ".exe" || ext == ".com") && exec.NE == magicnumber.UnknownNE:
if x8086 >= bytes {
s = "Dos command"
} else {
s = "Dos executable"
}
case (ext == ".exe" || ext == ".com") && exec.NE != magicnumber.NoneNE:
s = fmt.Sprintf("%s executable", exec)
case ext == ".exe" || ext == ".com":
case dosProg && exec.PE != magicnumber.UnknownPE:
s = exec.String() + " executable"
case dosProg && exec.NE == magicnumber.UnknownNE:
s = progrDos(x8086, bytes)
case dosProg && exec.NE != magicnumber.NoneNE:
s = exec.String() + " executable"
case dosProg:
s = "MS Dos program"
case ext == ".dll" && exec.PE != magicnumber.UnknownPE:
s = "Windows dynamic-link library"
Expand All @@ -146,12 +148,19 @@ func progr(exec magicnumber.Windows, ext, htm string, bytes int64) string {
s = "PE program data"
}
if y := exec.TimeDateStamp.Year(); y >= epochYear && y <= time.Now().Year() {
s += fmt.Sprintf(", built %s", exec.TimeDateStamp.Format("2006-01-2"))
s += ", built " + exec.TimeDateStamp.Format("2006-01-2")
}
htm += fmt.Sprintf(` <small class="">%s</small></div>`, s)
return htm
}

func progrDos(x8086 int, bytes int64) string {
if x8086 >= int(bytes) {
return "Dos command"
}
return "Dos executable"
}

// AlertURL returns the VirusTotal URL for the security alert for the file record.
// This will normally return an empty string unless the file has a security alert.
func AlertURL(art *models.File) string {
Expand Down Expand Up @@ -341,7 +350,6 @@ func (e *entry) parseImage(sign magicnumber.Signature, path string) bool {
}
switch sign {
case magicnumber.InterleavedBitmap:
fmt.Print("InterleavedBitmap")
r, _ := os.Open(path)
if r == nil {
return skipEntry
Expand All @@ -350,7 +358,7 @@ func (e *entry) parseImage(sign magicnumber.Signature, path string) bool {
x, y := magicnumber.IlbmDecode(r)
e.format = fmt.Sprintf("ILBM image, %dx%d", x, y)
default:
e.format = fmt.Sprintf("%s image", sign.Title())
e.format = sign.Title() + " image"
}
return !skipEntry
}
Expand Down Expand Up @@ -915,9 +923,9 @@ func JsdosUse(art *models.File) bool {
}
ext := filepath.Ext(strings.ToLower(art.Filename.String))
switch ext {
case ".exe", ".com":
case exe, com:
return true
case ".bat", ".cmd":
case bat, cmd:
return false
default:
return false
Expand Down
1 change: 1 addition & 0 deletions handler/app/internal/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func ImageSample(unid, previewDir string) template.HTML {
break
}
}
fmt.Println(name)
hash, err := helper.IntegrityFile(name)
if err != nil {
return template.HTML(`<div class="card-body">No preview image file</div>`)
Expand Down
16 changes: 11 additions & 5 deletions handler/app/internal/str/str_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package str_test

import (
"html/template"
"fmt"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -91,7 +92,7 @@ func TestUpdated(t *testing.T) {
func TestDemozooGetLink(t *testing.T) {
t.Parallel()
html := str.DemozooGetLink("", "", "", "")
assert.Equal(t, template.HTML("no id provided"), html)
assert.Empty(t, html)
fn := null.String{}
fs := null.Int64{}
dz := null.Int64{}
Expand All @@ -118,10 +119,15 @@ func TestDemozooGetLink(t *testing.T) {

func TestImageSample(t *testing.T) {
t.Parallel()
const missing = "No preview image file"
x := str.ImageSample("", "")
assert.Contains(t, x, "no such file")
assert.Contains(t, x, missing)
x = str.ImageSample("", "testdata/TEST.PNG")
assert.Contains(t, x, "no such file")
x = str.ImageSample("", "testdata/test")
assert.Contains(t, x, missing)
abs, err := filepath.Abs("../../testdata")
assert.NoError(t, err)
const filenameNoExt = "test"
x = str.ImageSample(filenameNoExt, abs)
fmt.Println(x)
assert.Contains(t, x, "sha384-SK3qCpS11QMhNxUUnyeUeWWXBMPORDgLTI")
}
31 changes: 21 additions & 10 deletions handler/htmx/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,28 +508,39 @@ func UploadPreview(c echo.Context, previewDir, thumbnailDir string) error {
return c.HTML(http.StatusInternalServerError,
"The magic number could not be found")
}
imgs := magicnumber.Images()
slices.Sort(imgs)
if slices.Contains(imgs, magic) {
if imagers(magic) {
if err := dirs.PictureImager(nil, dst.Name(), up.unid); err != nil {
return badRequest(c, err)
}
return c.String(http.StatusOK,
fmt.Sprintf("The new preview %s is in use, about to reload this page", file.Filename))
return reloader(c, file.Filename)
}
txts := magicnumber.Texts()
slices.Sort(txts)
if slices.Contains(txts, magic) {
if texters(magic) {
if err := dirs.TextImager(nil, dst.Name(), up.unid); err != nil {
return badRequest(c, err)
}
return c.String(http.StatusOK,
fmt.Sprintf("The new preview %s is in use, about to reload this page", file.Filename))
return reloader(c, file.Filename)
}
return c.HTML(http.StatusBadRequest,
"The chosen file is not a valid image or text file")
}

func imagers(magic magicnumber.Signature) bool {
imgs := magicnumber.Images()
slices.Sort(imgs)
return slices.Contains(imgs, magic)
}

func texters(magic magicnumber.Signature) bool {
txts := magicnumber.Texts()
slices.Sort(txts)
return slices.Contains(txts, magic)
}

func reloader(c echo.Context, filename string) error {
return c.String(http.StatusOK,
fmt.Sprintf("The new preview %s is in use, about to reload this page", filename))
}

// UploadReplacement is the file transfer handler that uploads, validates a new file upload
// and updates the existing artifact record with the new file information.
// The logger is optional and if nil then the function will not log any debug information.
Expand Down
4 changes: 3 additions & 1 deletion handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
const code = http.StatusMovedPermanently

// FilesRoutes defines the file locations and routes for the web server.
func (c Configuration) FilesRoutes(e *echo.Echo, db *sql.DB, logger *zap.SugaredLogger, public embed.FS) (*echo.Echo, error) {
func (c Configuration) FilesRoutes(e *echo.Echo, db *sql.DB, logger *zap.SugaredLogger,
public embed.FS,
) (*echo.Echo, error) {
if e == nil {
panic(fmt.Errorf("%w for files routes router", ErrRoutes))
}
Expand Down
5 changes: 3 additions & 2 deletions internal/command/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func ImagesDelete(unid string, dirs ...string) error {
}

// Pixelate appends the command line arguments for the convert command to transform an image into a PNG image.
// magick qqq.webp -scale 5% -scale 2000% zzz.webp
func (a *Args) Pixelate() {
// Create a canvas the size of the first images virtual canvas using the
// current -background color, and -compose each image in turn onto that canvas.
Expand All @@ -61,6 +60,9 @@ func (a *Args) Pixelate() {
*a = append(*a, scale2K...)
}

// ImagesPixelate converts the images in the specified directories to pixelated images.
// The unid is the unique identifier for the image file and shared between the preview
// and thumbnail images.
func ImagesPixelate(unid string, dirs ...string) error {
for _, dir := range dirs {
st, err := os.Stat(dir)
Expand All @@ -84,7 +86,6 @@ func ImagesPixelate(unid string, dirs ...string) error {
if err := RunQuiet(Magick, arg...); err != nil {
return fmt.Errorf("run pixelate convert %w", err)
}
//os.Remove(name)
}
}
return nil
Expand Down
37 changes: 37 additions & 0 deletions internal/config/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"github.com/Defacto2/server/internal/config/fixlha"
"github.com/Defacto2/server/internal/config/fixzip"
"github.com/Defacto2/server/internal/helper"
"github.com/Defacto2/server/internal/magicnumber"
"github.com/Defacto2/server/internal/postgres/models"
"github.com/Defacto2/server/internal/tags"
"github.com/google/uuid"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"go.uber.org/zap"
Expand Down Expand Up @@ -260,6 +262,37 @@ func (r Repair) rearchive(ctx context.Context, path, extra, uid string) error {
return nil
}

// TODO:
func (c Config) magics(ctx context.Context, exec boil.ContextExecutor) error {
empty := models.FileWhere.FileMagicType.EQ(null.StringFrom(""))
generic := models.FileWhere.FileMagicType.EQ(null.StringFrom("application/octet-stream"))
qmMods := []qm.QueryMod{empty, generic}
for _, mod := range qmMods {
nuls, err := models.Files(mod).All(ctx, exec)
if err != nil {
return fmt.Errorf("where file_magic_type is null: %w", err)
}
fmt.Fprintln(os.Stdout, "Files with null magic type:", len(nuls))
for _, f := range nuls {
// fmt.Fprintln(os.Stdout, f.UUID.String, f.Filename)
name := filepath.Join(c.AbsDownload, f.UUID.String)
x, err := os.Open(name)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
defer x.Close()
magic, err := magicnumber.Find(x)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
fmt.Fprintln(os.Stdout, magic.Title(), magic.String(), f.Filename.String)
}
}
return nil
}

// Assets, on startup check the file system directories for any invalid or unknown files.
// These specifically match the base filename against the UUID column in the database.
// When there is no matching UUID, the file is considered orphaned and these are moved
Expand Down Expand Up @@ -361,6 +394,10 @@ func (c Config) RepairAssets(ctx context.Context, exec boil.ContextExecutor) err
if err := c.Archives(ctx, exec); err != nil {
return fmt.Errorf("repair archives %w", err)
}
// TODO:
// if err := c.Magics(ctx, exec); err != nil {
// return fmt.Errorf("repair magics %w", err)
// }
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/helper/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func Count(dir string) (int, error) {
// DiskUsage returns the total size of the files in the given directory.
func DiskUsage(path string) (int64, error) {
var size int64
var skipItem = errors.New("skip item")
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return skipItem
fmt.Fprintln(io.Discard, err)
return nil
}
if !info.IsDir() {
size += info.Size()
Expand Down
Loading

0 comments on commit dd0b580

Please sign in to comment.