Skip to content

Commit

Permalink
support editing socials
Browse files Browse the repository at this point in the history
  • Loading branch information
raymonstah committed May 4, 2024
1 parent 5c9f048 commit 36692b9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 24 deletions.
105 changes: 85 additions & 20 deletions cmd/imggen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@ package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"cloud.google.com/go/firestore"
"cloud.google.com/go/storage"
"github.com/raymonstah/asianamericanswiki/functions/api"
"github.com/raymonstah/asianamericanswiki/internal/cartoonize"
"github.com/raymonstah/asianamericanswiki/internal/humandao"
"github.com/urfave/cli/v2"
)

var opts struct {
Image string
Name string
Debug bool
AsIs bool
Webp bool
Dry bool
}

func main() {
app := &cli.App{
Name: "A CLI tool to cartoonize an image.",
Name: "A CLI tool to upload an image.",
Flags: []cli.Flag{
&cli.PathFlag{Name: "image", Required: true, Destination: &opts.Image},
&cli.PathFlag{Name: "image", Destination: &opts.Image},
&cli.StringFlag{Name: "name", Required: true, Destination: &opts.Name},
&cli.BoolFlag{Name: "debug", Destination: &opts.Debug},
&cli.BoolFlag{Name: "as-is", Destination: &opts.AsIs},
&cli.BoolFlag{Name: "webp", Destination: &opts.Webp},
&cli.BoolFlag{Name: "dry", Destination: &opts.Dry},
},
Action: run,
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -79,24 +80,88 @@ func (h *Handler) Do(ctx context.Context) error {
return fmt.Errorf("unable to get human: %w", err)
}

var raw []byte
id := human.ID
if !opts.AsIs {
cartoonizeClient := cartoonize.Client{
Debug: opts.Debug,
}
raw, err = cartoonizeClient.Do(opts.Image)
pathToImage := opts.Image

if opts.Webp {
tempDir, err := os.MkdirTemp(os.TempDir(), "webp")
if err != nil {
return fmt.Errorf("unable to cartoonize image for %v: %w", human.Name, err)
return fmt.Errorf("unable to create temp dir: %w", err)
}
} else {
raw, err = os.ReadFile(opts.Image)
if err != nil {
return err

// no image provided, use the image from Cloud Storage
if pathToImage == "" {
cloudStoragePath := filepath.Base(human.FeaturedImage)
fmt.Println("cloudStoragePath:", cloudStoragePath)
object := h.storageClient.Bucket(api.ImagesStorageBucket).Object(cloudStoragePath)
reader, err := object.NewReader(ctx)
if err != nil {
return fmt.Errorf("unable to read image from cloud storage path %v: %w", cloudStoragePath, err)
}
parts := strings.Split(cloudStoragePath, ".")
extension := ""
if len(parts) == 1 {
attrs, err := object.Attrs(ctx)
if err != nil {
return fmt.Errorf("unable to get image attrs: %w", err)
}
if strings.Contains(attrs.ContentType, "jpeg") {
extension = ".jpeg"
} else if strings.Contains(attrs.ContentType, "png") {
extension = ".png"
} else {
return fmt.Errorf("unsupported file type: %v", attrs.ContentType)
}
}
pathToImage = filepath.Join(tempDir, cloudStoragePath+extension)
dest, err := os.Create(pathToImage)
if err != nil {
return fmt.Errorf("unable to write image from cloud storage: %w", err)
}
fmt.Println("wrote image from cloud storage to temp dir", pathToImage)
if _, err := io.Copy(dest, reader); err != nil {
return fmt.Errorf("unable to copy image from cloud storage: %w", err)
}
defer func() {
if len(parts) == 1 {
fmt.Println("overwrite happened -- skipping delete")
return
}
fmt.Printf("deleting old image %v", cloudStoragePath)
if err := object.Delete(ctx); err != nil {
log.Default().Panic("error deleting image", err)
}
}()

}

fileName := filepath.Base(pathToImage)
fileNameParts := strings.Split(fileName, ".")
fileNameWithoutExtension := fileNameParts[0]
webpImage := fileNameWithoutExtension + ".webp"
sourceImagePath := filepath.Join(tempDir, fileName)
pathToWebp := filepath.Join(tempDir, webpImage)
args := []string{"-path", tempDir, "-format", "webp", "-quality", "10", sourceImagePath}
cmd := exec.Command("mogrify", args...)
if err := cmd.Run(); err != nil {
fmt.Printf("mogrify %v\n", strings.Join(args, " "))
return fmt.Errorf("unable to convert image to webp: %w", err)
}
fmt.Printf("wrote webp image to %v\n", pathToWebp)
pathToImage = pathToWebp
}

raw, err := os.ReadFile(pathToImage)
if err != nil {
return err
}

if opts.Dry {
fmt.Println("dry mode detected -- exiting.")
return nil
}

imgName := fmt.Sprintf("%v.jpg", id)
imgName := fmt.Sprintf("%v", id)
obj := h.storageClient.Bucket(api.ImagesStorageBucket).Object(imgName)
writer := obj.NewWriter(ctx)
if _, err := writer.Write(raw); err != nil {
Expand Down
17 changes: 16 additions & 1 deletion functions/api/server/public/templates/humans-id-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h1 class="text-3xl font-extrabold my-4 text-center">
<input
class="border p-2 rounded dark:bg-gray-900"
type="text"
name="twitter"
name="x"
value="{{ .Human.Socials.X }}"
/>
</div>
Expand Down Expand Up @@ -124,6 +124,21 @@ <h1 class="text-3xl font-extrabold my-4 text-center">
/>
</div>
</div>
<div class="flex flex-row">
<div class="inset-y-0 start-0 flex items-center ps-3.5">
<svg xmlns="http://www.w3.org/2000/svg"
height="1.5em"
fill="currentColor"
viewBox="0 0 448 512">
<!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M224.1 141c-63.6 0-114.9 51.3-114.9 114.9s51.3 114.9 114.9 114.9S339 319.5 339 255.9 287.7 141 224.1 141zm0 189.6c-41.1 0-74.7-33.5-74.7-74.7s33.5-74.7 74.7-74.7 74.7 33.5 74.7 74.7-33.6 74.7-74.7 74.7zm146.4-194.3c0 14.9-12 26.8-26.8 26.8-14.9 0-26.8-12-26.8-26.8s12-26.8 26.8-26.8 26.8 12 26.8 26.8zm76.1 27.2c-1.7-35.9-9.9-67.7-36.2-93.9-26.2-26.2-58-34.4-93.9-36.2-37-2.1-147.9-2.1-184.9 0-35.8 1.7-67.6 9.9-93.9 36.1s-34.4 58-36.2 93.9c-2.1 37-2.1 147.9 0 184.9 1.7 35.9 9.9 67.7 36.2 93.9s58 34.4 93.9 36.2c37 2.1 147.9 2.1 184.9 0 35.9-1.7 67.7-9.9 93.9-36.2 26.2-26.2 34.4-58 36.2-93.9 2.1-37 2.1-147.8 0-184.8zM398.8 388c-7.8 19.6-22.9 34.7-42.6 42.6-29.5 11.7-99.5 9-132.1 9s-102.7 2.6-132.1-9c-19.6-7.8-34.7-22.9-42.6-42.6-11.7-29.5-9-99.5-9-132.1s-2.6-102.7 9-132.1c7.8-19.6 22.9-34.7 42.6-42.6 29.5-11.7 99.5-9 132.1-9s102.7-2.6 132.1 9c19.6 7.8 34.7 22.9 42.6 42.6 11.7 29.5 9 99.5 9 132.1s2.7 102.7-9 132.1z"/></svg>
<input
class="border p-2 rounded dark:bg-gray-900"
type="text"
name="instagram"
value="{{ .Human.Socials.Instagram }}"
/>
</div>
</div>

<textarea
name="description"
Expand Down
17 changes: 14 additions & 3 deletions functions/api/server/server-html.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,15 @@ func (s *ServerHTML) HandlerHumanUpdate(w http.ResponseWriter, r *http.Request)
return err
}

description := strings.TrimSpace(r.Form.Get("description"))
var rawImage []byte
var imageExtension string
var (
description = strings.TrimSpace(r.Form.Get("description"))
x = strings.TrimSpace(r.Form.Get("x"))
instagram = strings.TrimSpace(r.Form.Get("instagram"))
website = strings.TrimSpace(r.Form.Get("website"))
imdb = strings.TrimSpace(r.Form.Get("imdb"))
rawImage []byte
imageExtension string
)
file, header, err := r.FormFile("featured_image")
if err != http.ErrMissingFile {
if err != nil {
Expand Down Expand Up @@ -635,6 +641,11 @@ func (s *ServerHTML) HandlerHumanUpdate(w http.ResponseWriter, r *http.Request)
}

human.Description = description
human.Socials.X = x
human.Socials.Instagram = instagram
human.Socials.Website = website
human.Socials.IMDB = imdb

objectID := fmt.Sprintf("%v%v", human.ID, imageExtension)
if len(rawImage) > 0 {
obj := s.storageClient.Bucket(api.ImagesStorageBucket).Object(objectID)
Expand Down

0 comments on commit 36692b9

Please sign in to comment.