File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,12 @@ resource "mailform_pdf" "example" {
3535 filename = "./test.pdf"
3636}
3737
38+ // Convert image to pdf for postcards
39+ resource "mailform_pdf" "example" {
40+ image_filename = "./test_image.jpg"
41+ filename = "./test_image.pdf"
42+ }
43+
3844// Create mail order
3945resource "mailform_order" "example" {
4046 pdf_file = mailform_pdf.example.filename
Original file line number Diff line number Diff line change 44 "context"
55 "crypto/sha1"
66 "encoding/hex"
7+ "errors"
78 "io/ioutil"
9+ "net/http"
810 "os"
911
1012 "github.com/hashicorp/terraform-plugin-log/tflog"
@@ -56,6 +58,33 @@ func resourcePDF() *schema.Resource {
5658 "header" ,
5759 "content" ,
5860 },
61+ ValidateFunc : func (val any , key string ) (warns []string , errs []error ) {
62+ buf := make ([]byte , 512 )
63+
64+ imageFilename := val .(string )
65+ file , err := os .Open (imageFilename )
66+ if err != nil {
67+ errs = append (errs , err )
68+ return warns , errs
69+ }
70+
71+ defer file .Close ()
72+
73+ _ , err = file .Read (buf )
74+ if err != nil {
75+ errs = append (errs , err )
76+ return warns , errs
77+ }
78+
79+ contentType := http .DetectContentType (buf )
80+
81+ if contentType != "image/png" && contentType != "image/jpeg" {
82+ errs = append (errs , errors .New ("image file is not a valid image" ))
83+ return warns , errs
84+ }
85+
86+ return warns , errs
87+ },
5988 },
6089 },
6190 }
You can’t perform that action at this time.
0 commit comments