Skip to content

Commit

Permalink
intro uploader placeholder.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Apr 2, 2024
1 parent f8beadf commit 93fdc31
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
15 changes: 14 additions & 1 deletion assets/js/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@
throw new Error("The demozoo-submission element is null.");
}

const introM = document.getElementById("uploader-intro");
if (introM == null) {
throw new Error("The uploader-intro element is null.");
}
introM.addEventListener("shown.bs.modal", function () {
introInput.focus();
});
const introModal = new bootstrap.Modal(introM);
const introInput = document.getElementById("uploader-intro-file");
if (introInput == null) {
throw new Error("The uploader-intro-file element is null.");
}

const txtM = document.getElementById("uploaderText");
const imgM = document.getElementById("uploaderImg");
const magM = document.getElementById("uploaderMag");
Expand Down Expand Up @@ -99,7 +112,7 @@
pouetModal.show();
break;
case intro:
demozooModal.show();
introModal.show();
break;
case nfo:
txtModal.show();
Expand Down
67 changes: 67 additions & 0 deletions handler/htmx/htmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
package htmx

import (
"context"
"crypto/sha512"
"embed"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"os"
"strings"

"github.com/Defacto2/releaser"
"github.com/Defacto2/releaser/initialism"
"github.com/Defacto2/releaser/name"
"github.com/Defacto2/server/handler/app"
"github.com/Defacto2/server/internal/postgres"
"github.com/Defacto2/server/model"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.uber.org/zap"
Expand Down Expand Up @@ -40,9 +48,68 @@ func Routes(logr *zap.SugaredLogger, e *echo.Echo) *echo.Echo {
submit.POST("/search/releaser", func(x echo.Context) error {
return SearchReleaser(logr, x)
})
submit.POST("/uploader/intro", func(x echo.Context) error {
return holder(x)
})
return e
}

func holder(c echo.Context) error {
// Source
input, err := c.FormFile("uploader-intro-file")
if err != nil {
return err
}
src, err := input.Open()
if err != nil {
return err
}
defer src.Close()

hasher := sha512.New384()
if _, err := io.Copy(hasher, src); err != nil {
return err
}
sum := hasher.Sum(nil)
fmt.Printf("%x; %s\n", sum, input.Filename)

db, err := postgres.ConnectDB()
if err != nil {
return ErrDB
}
defer db.Close()
ctx := context.Background()

if exist, err := model.ExistsHash(ctx, db, sum); err != nil {
return err
} else if exist {
return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s already exists.</p>", input.Filename))
}

// reopen the file
src, err = input.Open()
if err != nil {
return err
}
defer src.Close()
// Destination
dst, err := os.CreateTemp("tmp", "upload-*.zip")
//dst, err := os.Create(file.Filename)
if err != nil {
return err
}
defer dst.Close()

// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}

return c.HTML(http.StatusOK,
fmt.Sprintf("<p>File %s uploaded successfully with fields.</p><p>%s</p>", input.Filename, dst.Name()))

}

// GlobTo returns the path to the template file.
func GlobTo(name string) string {
const pathSeparator = "/"
Expand Down
11 changes: 11 additions & 0 deletions model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func ExistFile(ctx context.Context, db *sql.DB, id int64) (bool, error) {
return models.Files(models.FileWhere.ID.EQ(id), qm.WithDeleted()).Exists(ctx, db)
}

// ExistsHash returns true if the file record exists in the database using a SHA-384 hash.
func ExistsHash(ctx context.Context, db *sql.DB, sha384 []byte) (bool, error) {
if db == nil {
return false, ErrDB
}
hash := fmt.Sprintf("%x", sha384)
// todo validate sha384 is not empty, is valid
strong := null.String{String: hash, Valid: true}
return models.Files(models.FileWhere.FileIntegrityStrong.EQ(strong), qm.WithDeleted()).Exists(ctx, db)
}

// FindFile retrieves a single file record from the database using the record key.
// This function will also return records that have been marked as deleted.
func FindFile(ctx context.Context, db *sql.DB, id int64) (*models.File, error) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/uploader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion view/app/layout_uploader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploader-pouet">Pouët</a></li>
<li><hr class="dropdown-divider"></li>
<li><h6 class="dropdown-header">Upload files</h6></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploaderIntro">Intro, cracktro, bbstro</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploader-intro">Intro, cracktro, bbstro</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploaderText">Text, ANSI or NFO</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploaderImg">Image or photo</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#uploaderMag">Magazine</a></li>
Expand Down
Loading

0 comments on commit 93fdc31

Please sign in to comment.