-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagemagick.go
70 lines (55 loc) · 1.54 KB
/
imagemagick.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package imagemagick
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"cloud.google.com/go/storage"
)
var (
storageClient *storage.Client
)
func init() {
var err error
storageClient, err = storage.NewClient(context.Background())
if err != nil {
log.Fatalf("storage.NewClient: %v", err)
}
}
// GCSEvent is the payload of a GCS event.
type GCSEvent struct {
Bucket string `json:"bucket"`
Name string `json:"name"`
}
// ImageConvert blurs offensive images uploaded to GCS.
func ImageConvert(ctx context.Context, e GCSEvent) error {
outputBucket := os.Getenv("THUMBNAILED")
if outputBucket == "" {
return errors.New("THUMBNAILED must be set")
}
return blur(ctx, e.Bucket, outputBucket, e.Name)
}
// [END functions_imagemagick_analyze]
// image convert from gs://inputBucket/name to gs://outputBucket/name.
func blur(ctx context.Context, inputBucket, outputBucket, name string) error {
inputBlob := storageClient.Bucket(inputBucket).Object(name)
r, err := inputBlob.NewReader(ctx)
if err != nil {
return fmt.Errorf("NewReader: %v", err)
}
outputBlob := storageClient.Bucket(outputBucket).Object(name)
w := outputBlob.NewWriter(ctx)
defer w.Close()
// Use - as input and output to use stdin and stdout.
cmd := exec.Command("convert", "-", "-resize", "30%", "-")
cmd.Stdin = r
cmd.Stdout = w
if err := cmd.Run(); err != nil {
return fmt.Errorf("cmd.Run: %v", err)
}
log.Printf("Blurred image uploaded to gs://%s/%s", outputBlob.BucketName(), outputBlob.ObjectName())
return nil
}
// [END functions_imagemagick_blur]