github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/property.go (about)

     1  /*
     2  	Copyright 2020 The pdfcpu Authors.
     3  
     4  	Licensed under the Apache License, Version 2.0 (the "License");
     5  	you may not use this file except in compliance with the License.
     6  	You may obtain a copy of the License at
     7  
     8  		http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  	Unless required by applicable law or agreed to in writing, software
    11  	distributed under the License is distributed on an "AS IS" BASIS,
    12  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  	See the License for the specific language governing permissions and
    14  	limitations under the License.
    15  */
    16  
    17  package api
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  // Properties returns rs's properties as recorded in infoDict.
    29  func Properties(rs io.ReadSeeker, conf *model.Configuration) (map[string]string, error) {
    30  	if rs == nil {
    31  		return nil, errors.New("pdfcpu: ListProperties: missing rs")
    32  	}
    33  
    34  	if conf == nil {
    35  		conf = model.NewDefaultConfiguration()
    36  		conf.ValidationMode = model.ValidationRelaxed
    37  	}
    38  	conf.Cmd = model.LISTPROPERTIES
    39  
    40  	ctx, err := ReadValidateAndOptimize(rs, conf)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return ctx.Properties, nil
    46  }
    47  
    48  // AddProperties adds properties to rs's infodict and writes the result to w.
    49  func AddProperties(rs io.ReadSeeker, w io.Writer, properties map[string]string, conf *model.Configuration) error {
    50  	if rs == nil {
    51  		return errors.New("pdfcpu: AddProperties: missing rs")
    52  	}
    53  
    54  	if conf == nil {
    55  		conf = model.NewDefaultConfiguration()
    56  	} else {
    57  		conf.ValidationMode = model.ValidationRelaxed
    58  	}
    59  	conf.Cmd = model.ADDPROPERTIES
    60  
    61  	ctx, err := ReadValidateAndOptimize(rs, conf)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if err = pdfcpu.PropertiesAdd(ctx, properties); err != nil {
    67  		return err
    68  	}
    69  
    70  	return Write(ctx, w, conf)
    71  }
    72  
    73  // AddPropertiesFile adds properties to inFile's infodict and writes the result to outFile.
    74  func AddPropertiesFile(inFile, outFile string, properties map[string]string, conf *model.Configuration) (err error) {
    75  	var f1, f2 *os.File
    76  
    77  	if f1, err = os.Open(inFile); err != nil {
    78  		return err
    79  	}
    80  
    81  	tmpFile := inFile + ".tmp"
    82  	if outFile != "" && inFile != outFile {
    83  		tmpFile = outFile
    84  	}
    85  	if f2, err = os.Create(tmpFile); err != nil {
    86  		f1.Close()
    87  		return err
    88  	}
    89  
    90  	defer func() {
    91  		if err != nil {
    92  			f2.Close()
    93  			f1.Close()
    94  			os.Remove(tmpFile)
    95  			return
    96  		}
    97  		if err = f2.Close(); err != nil {
    98  			return
    99  		}
   100  		if err = f1.Close(); err != nil {
   101  			return
   102  		}
   103  		if outFile == "" || inFile == outFile {
   104  			err = os.Rename(tmpFile, inFile)
   105  		}
   106  	}()
   107  
   108  	return AddProperties(f1, f2, properties, conf)
   109  }
   110  
   111  // RemoveProperties deletes properties from rs's infodict and writes the result to w.
   112  func RemoveProperties(rs io.ReadSeeker, w io.Writer, properties []string, conf *model.Configuration) error {
   113  	if rs == nil {
   114  		return errors.New("pdfcpu: RemoveProperties: missing rs")
   115  	}
   116  
   117  	if conf == nil {
   118  		conf = model.NewDefaultConfiguration()
   119  	} else {
   120  		conf.ValidationMode = model.ValidationRelaxed
   121  	}
   122  	conf.Cmd = model.REMOVEPROPERTIES
   123  
   124  	ctx, err := ReadValidateAndOptimize(rs, conf)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	var ok bool
   130  	if ok, err = pdfcpu.PropertiesRemove(ctx, properties); err != nil {
   131  		return err
   132  	}
   133  	if !ok {
   134  		return errors.New("no property removed")
   135  	}
   136  
   137  	return Write(ctx, w, conf)
   138  }
   139  
   140  // RemovePropertiesFile deletes properties from inFile's infodict and writes the result to outFile.
   141  func RemovePropertiesFile(inFile, outFile string, properties []string, conf *model.Configuration) (err error) {
   142  	var f1, f2 *os.File
   143  
   144  	if f1, err = os.Open(inFile); err != nil {
   145  		return err
   146  	}
   147  
   148  	tmpFile := inFile + ".tmp"
   149  	if outFile != "" && inFile != outFile {
   150  		tmpFile = outFile
   151  	}
   152  	if f2, err = os.Create(tmpFile); err != nil {
   153  		f1.Close()
   154  		return err
   155  	}
   156  
   157  	defer func() {
   158  		if err != nil {
   159  			f2.Close()
   160  			f1.Close()
   161  			os.Remove(tmpFile)
   162  			return
   163  		}
   164  		if err = f2.Close(); err != nil {
   165  			return
   166  		}
   167  		if err = f1.Close(); err != nil {
   168  			return
   169  		}
   170  		if outFile == "" || inFile == outFile {
   171  			err = os.Rename(tmpFile, inFile)
   172  		}
   173  	}()
   174  
   175  	return RemoveProperties(f1, f2, properties, conf)
   176  }