-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor_extras2.go
More file actions
565 lines (498 loc) · 14.6 KB
/
Copy pathprocessor_extras2.go
File metadata and controls
565 lines (498 loc) · 14.6 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package main
import (
"archive/zip"
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"canivete/imagemeta"
"github.com/disintegration/imaging"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
)
func handleImgExifStrip(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxBatchUploadSize) {
return
}
defer cleanupMultipartForm(r)
var files []*multipart.FileHeader
if f := r.MultipartForm.File["images"]; len(f) > 0 {
files = f
} else if f := r.MultipartForm.File["image"]; len(f) > 0 {
files = f
}
if len(files) == 0 {
http.Error(w, "Nenhuma imagem enviada", http.StatusBadRequest)
return
}
if len(files) > 1 {
handleBatchImgExifStrip(w, r, files)
return
}
fileHeader := files[0]
file, err := fileHeader.Open()
if err != nil {
http.Error(w, "Erro ao abrir imagem", http.StatusBadRequest)
return
}
defer file.Close()
tmpOutput, err := os.CreateTemp("", "stripped-*.tmp")
if err != nil {
internalError(w, "Erro temporário", err)
return
}
defer os.Remove(tmpOutput.Name())
defer tmpOutput.Close()
report, err := imagemeta.StripAISignatures(r.Context(), file, tmpOutput, "")
if err != nil {
log.Printf("Erro ao remover metadados: %v", err)
http.Error(w, "Erro ao processar imagem, certifique-se de ser PNG, JPG ou WEBP válido", http.StatusBadRequest)
return
}
var ext, contentType string
switch report.Format {
case "jpeg", "jpg":
ext, contentType = "jpg", "image/jpeg"
case "png":
ext, contentType = "png", "image/png"
case "webp":
ext, contentType = "webp", "image/webp"
default:
http.Error(w, "Formato não suportado para anonimização", http.StatusBadRequest)
return
}
if _, err := tmpOutput.Seek(0, io.SeekStart); err != nil {
internalError(w, "Erro ao preparar download", err)
return
}
originalName := strings.TrimSuffix(filepath.Base(fileHeader.Filename), filepath.Ext(fileHeader.Filename))
setDownloadHeaders(w, contentType, fmt.Sprintf("%s-anon.%s", safeFilename(originalName), ext))
if _, err := io.Copy(w, tmpOutput); err != nil {
log.Printf("Erro ao enviar imagem anonimizada: %v", err)
}
}
func handleBatchImgExifStrip(w http.ResponseWriter, r *http.Request, files []*multipart.FileHeader) {
if len(files) > maxBatchFiles {
http.Error(w, "Quantidade máxima de imagens excedida", http.StatusBadRequest)
return
}
zipFile, err := os.CreateTemp("", "anon-images-*.zip")
if err != nil {
internalError(w, "Erro ao preparar arquivo ZIP", err)
return
}
defer os.Remove(zipFile.Name())
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
var zipMutex sync.Mutex
var errGroup error
sem := make(chan struct{}, 4)
var wg sync.WaitGroup
var errorsLog []string
for _, fileHeader := range files {
wg.Add(1)
go func(fh *multipart.FileHeader) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
file, err := fh.Open()
if err != nil {
zipMutex.Lock()
errorsLog = append(errorsLog, fmt.Sprintf("%s: Erro ao abrir imagem", fh.Filename))
zipMutex.Unlock()
return
}
defer file.Close()
var buf bytes.Buffer
report, err := imagemeta.StripAISignatures(r.Context(), file, &buf, "")
if err != nil {
zipMutex.Lock()
errorsLog = append(errorsLog, fmt.Sprintf("%s: Erro ao remover metadados (%v)", fh.Filename, err))
zipMutex.Unlock()
return
}
var ext string
switch report.Format {
case "jpeg", "jpg":
ext = "jpg"
case "png":
ext = "png"
case "webp":
ext = "webp"
default:
zipMutex.Lock()
errorsLog = append(errorsLog, fmt.Sprintf("%s: Formato não suportado (%s)", fh.Filename, report.Format))
zipMutex.Unlock()
return
}
zipMutex.Lock()
defer zipMutex.Unlock()
base := strings.TrimSuffix(filepath.Base(fh.Filename), filepath.Ext(fh.Filename))
safe := safeFilename(base)
if safe == "" {
safe = "imagem"
}
fw, err := zipWriter.Create(fmt.Sprintf("%s-anon.%s", safe, ext))
if err != nil {
errGroup = err
return
}
_, _ = io.Copy(fw, &buf)
}(fileHeader)
}
wg.Wait()
if errGroup != nil {
internalError(w, "Erro ao compactar imagens anonimizadas", errGroup)
return
}
if len(errorsLog) > 0 {
fw, err := zipWriter.Create("relatorio_erros.txt")
if err == nil {
_, _ = fw.Write([]byte(strings.Join(errorsLog, "\n")))
}
}
if err := zipWriter.Close(); err != nil {
internalError(w, "Erro ao finalizar arquivo ZIP", err)
return
}
if err := serveDownloadFile(w, zipFile, "application/zip", "imagens_anonimizadas.zip"); err != nil {
log.Printf("Erro ao enviar imagens anonimizadas: %v", err)
}
}
func handleImgRotate(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxUploadSize) {
return
}
defer cleanupMultipartForm(r)
file, _, err := r.FormFile("image")
if err != nil {
http.Error(w, "Imagem não enviada", http.StatusBadRequest)
return
}
defer file.Close()
img, format, err := decodeImage(file)
if err != nil {
http.Error(w, "Imagem inválida ou acima dos limites permitidos", http.StatusBadRequest)
return
}
var result image.Image
switch r.FormValue("action") {
case "rot90":
result = imaging.Rotate90(img)
case "rot180":
result = imaging.Rotate180(img)
case "rot270":
result = imaging.Rotate270(img)
case "fliph":
result = imaging.FlipH(img)
case "flipv":
result = imaging.FlipV(img)
default:
http.Error(w, "Ação de rotação inválida", http.StatusBadRequest)
return
}
if format == "jpeg" {
setDownloadHeaders(w, "image/jpeg", "imagem-rotacionada.jpg")
if err := jpeg.Encode(w, result, &jpeg.Options{Quality: 95}); err != nil {
log.Printf("Erro ao codificar JPEG rotacionado: %v", err)
}
return
}
setDownloadHeaders(w, "image/png", "imagem-rotacionada.png")
if err := png.Encode(w, result); err != nil {
log.Printf("Erro ao codificar PNG rotacionado: %v", err)
}
}
func handleBase64(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxUploadSize) {
return
}
defer cleanupMultipartForm(r)
switch r.FormValue("mode") {
case "encode":
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Arquivo não enviado", http.StatusBadRequest)
return
}
defer file.Close()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
encoder := base64.NewEncoder(base64.StdEncoding, w)
if _, err := io.Copy(encoder, file); err != nil {
log.Printf("Erro ao codificar Base64: %v", err)
_ = encoder.Close()
return
}
if err := encoder.Close(); err != nil {
log.Printf("Erro ao finalizar Base64: %v", err)
}
case "decode":
encoded := strings.TrimSpace(r.FormValue("text"))
if encoded == "" {
http.Error(w, "Texto Base64 vazio", http.StatusBadRequest)
return
}
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
http.Error(w, "Texto Base64 inválido", http.StatusBadRequest)
return
}
setDownloadHeaders(w, "application/octet-stream", "decoded.bin")
// #nosec G705 -- resposta binária é attachment, application/octet-stream e nosniff.
_, _ = w.Write(decoded)
default:
http.Error(w, "Modo Base64 inválido", http.StatusBadRequest)
}
}
func handleMinify(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxUploadSize) {
return
}
defer cleanupMultipartForm(r)
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Arquivo não enviado", http.StatusBadRequest)
return
}
defer file.Close()
language := r.FormValue("lang")
if language == "" {
switch strings.ToLower(filepath.Ext(header.Filename)) {
case ".html", ".htm":
language = "text/html"
case ".css":
language = "text/css"
case ".js", ".mjs":
language = "application/javascript"
case ".json":
language = "application/json"
}
}
allowed := map[string]bool{
"text/html": true,
"text/css": true,
"application/javascript": true,
"application/json": true,
}
if !allowed[language] {
http.Error(w, "Formato não suportado; use HTML, CSS, JS ou JSON", http.StatusBadRequest)
return
}
minifier := minify.New()
minifier.AddFunc("text/html", html.Minify)
minifier.AddFunc("text/css", css.Minify)
minifier.AddFunc("application/javascript", js.Minify)
minifier.AddFunc("application/json", json.Minify)
var output bytes.Buffer
if err := minifier.Minify(language, &output, file); err != nil {
http.Error(w, "Arquivo inválido para o formato selecionado", http.StatusBadRequest)
return
}
setDownloadHeaders(w, "text/plain; charset=utf-8", "minified_"+safeFilename(header.Filename))
_, _ = output.WriteTo(w)
}
func handlePdfRotate(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxPDFUploadSize) {
return
}
defer cleanupMultipartForm(r)
file, _, err := r.FormFile("pdf")
if err != nil {
http.Error(w, "PDF não enviado", http.StatusBadRequest)
return
}
defer file.Close()
degrees, err := strconv.Atoi(r.FormValue("degrees"))
if err != nil || (degrees != 90 && degrees != 180 && degrees != 270) {
http.Error(w, "Rotação deve ser 90, 180 ou 270 graus", http.StatusBadRequest)
return
}
pages := strings.TrimSpace(r.FormValue("pages"))
if len(pages) > 200 {
http.Error(w, "Seleção de páginas muito longa", http.StatusBadRequest)
return
}
var pageSelection []string
if pages != "" {
pageSelection = []string{pages}
}
output, err := os.CreateTemp("", "canivete-rotated-*.pdf")
if err != nil {
internalError(w, "Erro ao preparar PDF de saída", err)
return
}
defer func() {
_ = output.Close()
_ = os.Remove(output.Name())
}()
if err := api.Rotate(file, output, degrees, pageSelection, nil); err != nil {
log.Printf("Erro ao girar PDF: %v", err)
http.Error(w, "PDF ou seleção de páginas inválida", http.StatusBadRequest)
return
}
if err := serveDownloadFile(w, output, "application/pdf", "rotated.pdf"); err != nil {
log.Printf("Erro ao enviar PDF rotacionado: %v", err)
}
}
func writeICO(w io.Writer, images [][]byte) error {
if len(images) == 0 || len(images) > int(^uint16(0)) {
return fmt.Errorf("quantidade inválida de imagens no ICO: %d", len(images))
}
headerSize := 6 + 16*len(images)
if uint64(headerSize) > uint64(^uint32(0)) {
return fmt.Errorf("cabeçalho ICO excede o limite de 32 bits")
}
if err := binary.Write(w, binary.LittleEndian, uint16(0)); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint16(1)); err != nil {
return err
}
// #nosec G115 -- len(images) foi validado contra o limite de uint16 acima.
if err := binary.Write(w, binary.LittleEndian, uint16(len(images))); err != nil {
return err
}
// #nosec G115 -- headerSize foi validado contra o limite de uint32 acima.
offset := uint32(headerSize)
for _, data := range images {
config, err := png.DecodeConfig(bytes.NewReader(data))
if err != nil {
return err
}
if config.Width < 1 || config.Width > 256 || config.Height < 1 || config.Height > 256 {
return fmt.Errorf("dimensão inválida no ICO: %dx%d", config.Width, config.Height)
}
if uint64(len(data)) > uint64(^uint32(0)) {
return fmt.Errorf("imagem interna do ICO excede o limite de 32 bits")
}
// O formato ICO representa a dimensão 256 pelo byte zero.
// #nosec G115 -- dimensões validadas no intervalo 1..256.
entry := []byte{byte(config.Width % 256), byte(config.Height % 256), 0, 0}
if _, err := w.Write(entry); err != nil {
return err
}
for _, value := range []uint16{1, 32} {
if err := binary.Write(w, binary.LittleEndian, value); err != nil {
return err
}
}
// #nosec G115 -- tamanho validado contra o limite de uint32 acima.
dataSize := uint32(len(data))
if err := binary.Write(w, binary.LittleEndian, dataSize); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, offset); err != nil {
return err
}
if uint64(offset)+uint64(dataSize) > uint64(^uint32(0)) {
return fmt.Errorf("arquivo ICO excede o limite de 32 bits")
}
offset += dataSize
}
for _, data := range images {
if _, err := w.Write(data); err != nil {
return err
}
}
return nil
}
func handleImgToIco(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxUploadSize) {
return
}
defer cleanupMultipartForm(r)
file, _, err := r.FormFile("image")
if err != nil {
http.Error(w, "Imagem não enviada", http.StatusBadRequest)
return
}
defer file.Close()
img, _, err := decodeImage(file)
if err != nil {
http.Error(w, "Imagem inválida ou acima dos limites permitidos", http.StatusBadRequest)
return
}
sizes := []int{16, 32, 48}
pngImages := make([][]byte, 0, len(sizes))
for _, size := range sizes {
resized := imaging.Fill(img, size, size, imaging.Center, imaging.Lanczos)
var output bytes.Buffer
if err := png.Encode(&output, resized); err != nil {
internalError(w, "Erro ao gerar ícone", err)
return
}
pngImages = append(pngImages, output.Bytes())
}
var ico bytes.Buffer
if err := writeICO(&ico, pngImages); err != nil {
internalError(w, "Erro ao gerar ícone", err)
return
}
setDownloadHeaders(w, "image/x-icon", "favicon.ico")
_, _ = ico.WriteTo(w)
}
func handlePdfWatermark(w http.ResponseWriter, r *http.Request) {
if !parseMultipartForm(w, r, maxPDFUploadSize) {
return
}
defer cleanupMultipartForm(r)
file, _, err := r.FormFile("pdf")
if err != nil {
http.Error(w, "PDF não enviado", http.StatusBadRequest)
return
}
defer file.Close()
watermarkText := strings.TrimSpace(r.FormValue("text"))
if watermarkText == "" {
watermarkText = "Watermark"
}
if len([]rune(watermarkText)) > 200 {
http.Error(w, "Texto da marca d'água deve ter no máximo 200 caracteres", http.StatusBadRequest)
return
}
watermark, err := api.TextWatermark(
watermarkText,
"font:Helvetica, points:48, rot:45, scale:1.0 abs, op:0.3",
true,
false,
types.POINTS,
)
if err != nil {
http.Error(w, "Texto de marca d'água inválido", http.StatusBadRequest)
return
}
output, err := os.CreateTemp("", "canivete-watermarked-*.pdf")
if err != nil {
internalError(w, "Erro ao preparar PDF de saída", err)
return
}
defer func() {
_ = output.Close()
_ = os.Remove(output.Name())
}()
if err := api.AddWatermarks(file, output, nil, watermark, nil); err != nil {
log.Printf("Erro ao adicionar marca d'água: %v", err)
http.Error(w, "PDF inválido ou não suportado", http.StatusBadRequest)
return
}
if err := serveDownloadFile(w, output, "application/pdf", "watermarked.pdf"); err != nil {
log.Printf("Erro ao enviar PDF com marca d'água: %v", err)
}
}