2025-02-28 13:11:53 +00:00
|
|
|
package documentclean
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Content interface {
|
2025-02-28 15:56:47 +00:00
|
|
|
isCorrupt(context.Context) *InvalidDocumentReason
|
2025-02-28 13:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) getContent(ctx context.Context, params *CleanParams, mimeType MimeType) (Content, error) {
|
|
|
|
|
resp, err := s.cfg.GetStoreClient().GetObject(ctx, &s3.GetObjectInput{
|
|
|
|
|
Bucket: ¶ms.Location.Bucket,
|
|
|
|
|
Key: ¶ms.Location.Key,
|
|
|
|
|
IfMatch: ¶ms.Hash,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
seek, err := ReaderToSeeker(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var content Content
|
|
|
|
|
switch mimeType {
|
|
|
|
|
case MimeTypePDF:
|
|
|
|
|
content = NewPDF(seek)
|
|
|
|
|
default:
|
|
|
|
|
return nil, errors.New("invalid mimetype")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ReaderToSeeker(readCloser io.ReadCloser) (io.ReadSeeker, error) {
|
|
|
|
|
data, err := io.ReadAll(readCloser)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readCloser.Close()
|
|
|
|
|
|
|
|
|
|
return bytes.NewReader(data), nil
|
|
|
|
|
}
|