@@ -426,53 +426,32 @@ func inlineImages(body string, ctx *mailCommentContext) (string, error) {
426
426
// Helper function to convert attachment source to data URI
427
427
428
428
func attachmentSrcToDataURI (attachmentPath string , ctx * mailCommentContext ) (string , error ) {
429
- // Extract the UUID from the attachment path
430
429
parts := strings .Split (attachmentPath , "/attachments/" )
431
- attachmentUUID := ""
432
- if len (parts ) > 1 {
433
- attachmentUUID = parts [1 ]
434
- } else {
430
+ if len (parts ) <= 1 {
435
431
return "" , fmt .Errorf ("Invalid attachment path: %s" , attachmentPath )
436
432
}
437
- // Retrieve the attachment using the UUID
433
+
434
+ attachmentUUID := parts [len (parts )- 1 ]
438
435
attachment , err := repo_model .GetAttachmentByUUID (ctx , attachmentUUID )
439
- log .Trace ("attachmentUUID: %s" , attachmentUUID )
440
436
if err != nil {
441
437
return "" , err
442
438
}
443
439
444
- // log attachment.DownloadPath
445
- log .Trace ("attachment.DownloadURL(): %s" , attachment .DownloadURL ())
446
-
447
440
fr , err := storage .Attachments .Open (attachment .RelativePath ())
448
441
if err != nil {
449
442
return "" , err
450
443
}
451
- defer func (fr storage.Object ) {
452
- err := fr .Close ()
453
- if err != nil {
454
- }
455
- }(fr )
444
+ defer fr .Close ()
456
445
457
- // fr is io.ReadSeeker
458
- // get this as bytes:
459
446
content := make ([]byte , attachment .Size )
460
- _ , err = fr .Read (content )
461
- if err != nil {
447
+ if _ , err := fr .Read (content ); err != nil {
462
448
return "" , err
463
449
}
464
450
465
- // Detect the MIME type
466
451
mimeType := http .DetectContentType (content )
467
-
468
- log .Trace ("MIME type: %s" , mimeType )
469
-
470
- // Encode the content to base64
471
452
encoded := base64 .StdEncoding .EncodeToString (content )
472
- log .Trace ("First 100 characters of encoded content: %s" , encoded [:100 ])
473
-
474
- // Construct the data URI
475
453
dataURI := fmt .Sprintf ("data:%s;base64,%s" , mimeType , encoded )
454
+
476
455
return dataURI , nil
477
456
}
478
457
0 commit comments