mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-14 11:22:15 +01:00
fix(pdgengines): encrypt behavior accross different engines
This commit is contained in:
@@ -270,6 +270,16 @@ func EncryptPdfStub(ctx *api.Context, engine gotenberg.PdfEngine, userPassword,
|
||||
for _, inputPath := range inputPaths {
|
||||
err := engine.Encrypt(ctx, ctx.Log(), inputPath, userPassword, ownerPassword)
|
||||
if err != nil {
|
||||
if errors.Is(err, gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported) {
|
||||
return api.WrapError(
|
||||
err,
|
||||
api.NewSentinelHttpError(
|
||||
http.StatusBadRequest,
|
||||
"Invalid form data: both 'userPassword' and 'ownerPassword' form fields must be provided and different",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Errorf("encrypt PDF '%s': %w", inputPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,13 +151,16 @@ func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
|
||||
return errors.New("user password cannot be empty")
|
||||
}
|
||||
|
||||
if ownerPassword == "" {
|
||||
ownerPassword = userPassword
|
||||
if ownerPassword == userPassword || ownerPassword == "" {
|
||||
return gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported
|
||||
}
|
||||
|
||||
// Create a temp output file in the same directory.
|
||||
tmpPath := inputPath + ".tmp"
|
||||
|
||||
var args []string
|
||||
args = append(args, inputPath)
|
||||
args = append(args, "output", inputPath)
|
||||
args = append(args, "output", tmpPath)
|
||||
args = append(args, "encrypt_128bit")
|
||||
args = append(args, "user_pw", userPassword)
|
||||
args = append(args, "owner_pw", ownerPassword)
|
||||
@@ -172,6 +175,11 @@ func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
|
||||
return fmt.Errorf("encrypt PDF with PDFtk: %w", err)
|
||||
}
|
||||
|
||||
err = os.Rename(tmpPath, inputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rename temporary output file with input file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -185,8 +185,8 @@ func (engine *QPdf) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
|
||||
var args []string
|
||||
args = append(args, inputPath)
|
||||
args = append(args, engine.globalArgs...)
|
||||
args = append(args, "--encrypt", userPassword, ownerPassword, "256", "--use-aes=y", "--")
|
||||
args = append(args, inputPath)
|
||||
args = append(args, "--replace-input")
|
||||
args = append(args, "--encrypt", userPassword, ownerPassword, "256", "--")
|
||||
|
||||
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user