github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/keyword.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  // Keywords returns the keywords of rs's info dict.
    29  func Keywords(rs io.ReadSeeker, conf *model.Configuration) ([]string, error) {
    30  	if rs == nil {
    31  		return nil, errors.New("pdfcpu: ListKeywords: missing rs")
    32  	}
    33  
    34  	if conf == nil {
    35  		conf = model.NewDefaultConfiguration()
    36  	} else {
    37  		conf.ValidationMode = model.ValidationRelaxed
    38  	}
    39  	conf.Cmd = model.LISTKEYWORDS
    40  
    41  	ctx, err := ReadValidateAndOptimize(rs, conf)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return pdfcpu.KeywordsList(ctx)
    47  }
    48  
    49  // AddKeywords adds keywords to rs's infodict and writes the result to w.
    50  func AddKeywords(rs io.ReadSeeker, w io.Writer, files []string, conf *model.Configuration) error {
    51  	if rs == nil {
    52  		return errors.New("pdfcpu: AddKeywords: missing rs")
    53  	}
    54  
    55  	if conf == nil {
    56  		conf = model.NewDefaultConfiguration()
    57  	} else {
    58  		conf.ValidationMode = model.ValidationRelaxed
    59  	}
    60  	conf.Cmd = model.ADDKEYWORDS
    61  
    62  	ctx, err := ReadValidateAndOptimize(rs, conf)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if err = pdfcpu.KeywordsAdd(ctx, files); err != nil {
    68  		return err
    69  	}
    70  
    71  	return Write(ctx, w, conf)
    72  }
    73  
    74  // AddKeywordsFile adds keywords to inFile's infodict and writes the result to outFile.
    75  func AddKeywordsFile(inFile, outFile string, files []string, conf *model.Configuration) (err error) {
    76  	var f1, f2 *os.File
    77  
    78  	if f1, err = os.Open(inFile); err != nil {
    79  		return err
    80  	}
    81  
    82  	tmpFile := inFile + ".tmp"
    83  	if outFile != "" && inFile != outFile {
    84  		tmpFile = outFile
    85  	}
    86  	if f2, err = os.Create(tmpFile); err != nil {
    87  		f1.Close()
    88  		return err
    89  	}
    90  
    91  	defer func() {
    92  		if err != nil {
    93  			f2.Close()
    94  			f1.Close()
    95  			os.Remove(tmpFile)
    96  			return
    97  		}
    98  		if err = f2.Close(); err != nil {
    99  			return
   100  		}
   101  		if err = f1.Close(); err != nil {
   102  			return
   103  		}
   104  		if outFile == "" || inFile == outFile {
   105  			err = os.Rename(tmpFile, inFile)
   106  		}
   107  	}()
   108  
   109  	return AddKeywords(f1, f2, files, conf)
   110  }
   111  
   112  // RemoveKeywords deletes keywords from rs's infodict and writes the result to w.
   113  func RemoveKeywords(rs io.ReadSeeker, w io.Writer, keywords []string, conf *model.Configuration) error {
   114  	if rs == nil {
   115  		return errors.New("pdfcpu: RemoveKeywords: missing rs")
   116  	}
   117  
   118  	if conf == nil {
   119  		conf = model.NewDefaultConfiguration()
   120  	} else {
   121  		conf.ValidationMode = model.ValidationRelaxed
   122  	}
   123  	conf.Cmd = model.REMOVEKEYWORDS
   124  
   125  	ctx, err := ReadValidateAndOptimize(rs, conf)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	var ok bool
   131  	if ok, err = pdfcpu.KeywordsRemove(ctx, keywords); err != nil {
   132  		return err
   133  	}
   134  	if !ok {
   135  		return errors.New("no keyword removed")
   136  	}
   137  
   138  	return Write(ctx, w, conf)
   139  }
   140  
   141  // RemoveKeywordsFile deletes keywords from inFile's infodict and writes the result to outFile.
   142  func RemoveKeywordsFile(inFile, outFile string, keywords []string, conf *model.Configuration) (err error) {
   143  	var f1, f2 *os.File
   144  
   145  	if f1, err = os.Open(inFile); err != nil {
   146  		return err
   147  	}
   148  
   149  	tmpFile := inFile + ".tmp"
   150  	if outFile != "" && inFile != outFile {
   151  		tmpFile = outFile
   152  	}
   153  	if f2, err = os.Create(tmpFile); err != nil {
   154  		f1.Close()
   155  		return err
   156  	}
   157  
   158  	defer func() {
   159  		if err != nil {
   160  			f2.Close()
   161  			f1.Close()
   162  			os.Remove(tmpFile)
   163  			return
   164  		}
   165  		if err = f2.Close(); err != nil {
   166  			return
   167  		}
   168  		if err = f1.Close(); err != nil {
   169  			return
   170  		}
   171  		if outFile == "" || inFile == outFile {
   172  			err = os.Rename(tmpFile, inFile)
   173  		}
   174  	}()
   175  
   176  	return RemoveKeywords(f1, f2, keywords, conf)
   177  }