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" {
35
35
filename = "./test.pdf"
36
36
}
37
37
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
+
38
44
// Create mail order
39
45
resource "mailform_order" "example" {
40
46
pdf_file = mailform_pdf.example.filename
Original file line number Diff line number Diff line change 4
4
"context"
5
5
"crypto/sha1"
6
6
"encoding/hex"
7
+ "errors"
7
8
"io/ioutil"
9
+ "net/http"
8
10
"os"
9
11
10
12
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -56,6 +58,33 @@ func resourcePDF() *schema.Resource {
56
58
"header" ,
57
59
"content" ,
58
60
},
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
+ },
59
88
},
60
89
},
61
90
}
You can’t perform that action at this time.
0 commit comments