github.com/boki/go-xmp@v1.0.1/cmd/xmpinfo.go (about)

     1  // Copyright (c) 2017-2018 Alexander Eichhorn
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"encoding/json"
    19  	"flag"
    20  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  
    26  	_ "trimmer.io/go-xmp/models"
    27  	"trimmer.io/go-xmp/models/dc"
    28  	"trimmer.io/go-xmp/models/xmp_base"
    29  	"trimmer.io/go-xmp/xmp"
    30  )
    31  
    32  var (
    33  	debug bool
    34  	quiet bool
    35  	fjson bool
    36  	fxmp  bool
    37  	fpath bool
    38  	forig bool
    39  	fall  bool
    40  )
    41  
    42  func init() {
    43  	flag.BoolVar(&debug, "debug", false, "enable debugging")
    44  	flag.BoolVar(&quiet, "quiet", false, "don't output anything")
    45  	flag.BoolVar(&fjson, "json", false, "enable JSON output")
    46  	flag.BoolVar(&fxmp, "xmp", false, "enable XMP output")
    47  	flag.BoolVar(&fpath, "path", false, "enable XMP/Path output")
    48  	flag.BoolVar(&forig, "orig", false, "enable original XMP output")
    49  	flag.BoolVar(&fall, "all", false, "output all embedded xmp documents")
    50  }
    51  
    52  func fail(v interface{}) {
    53  	fmt.Printf("Error: %s in file %s\n", v, flag.Arg(0))
    54  	os.Exit(1)
    55  }
    56  
    57  func out(b []byte) {
    58  	if quiet {
    59  		return
    60  	}
    61  	fmt.Println(string(b))
    62  }
    63  
    64  func marshal(d *xmp.Document) []byte {
    65  	b, err := xmp.MarshalIndent(d, "", "  ")
    66  	if err != nil {
    67  		fail(err)
    68  	}
    69  	return b
    70  }
    71  
    72  func unmarshal(v []byte) *xmp.Document {
    73  	d := &xmp.Document{}
    74  	if err := xmp.Unmarshal(v, d); err != nil {
    75  		fail(err)
    76  	}
    77  	return d
    78  }
    79  
    80  func main() {
    81  	flag.Parse()
    82  
    83  	if debug {
    84  		xmp.SetLogLevel(xmp.LogLevelDebug)
    85  	}
    86  
    87  	// output original when no option is selected
    88  	if !fjson && !fxmp && !fpath && !forig && !quiet {
    89  		forig = true
    90  	}
    91  
    92  	var b []byte
    93  
    94  	if flag.NArg() > 0 {
    95  		filename := flag.Arg(0)
    96  		f, err := os.Open(filename)
    97  		if err != nil {
    98  			fail(err)
    99  		}
   100  		defer f.Close()
   101  
   102  		switch filepath.Ext(filename) {
   103  		case ".xmp":
   104  			b, err = ioutil.ReadAll(f)
   105  			if err != nil {
   106  				fail(err)
   107  			}
   108  
   109  		default:
   110  			bb, err := xmp.ScanPackets(f)
   111  			if err != nil && err != io.EOF {
   112  				fail(err)
   113  			}
   114  			if err == io.EOF {
   115  				return
   116  			}
   117  			if forig && fall {
   118  				for _, b := range bb {
   119  					out(b)
   120  				}
   121  				return
   122  			}
   123  			b = bb[0]
   124  		}
   125  
   126  	} else {
   127  		// fill the document with some info
   128  		s := xmp.NewDocument()
   129  		s.AddModel(&xmpbase.XmpBase{
   130  			CreatorTool: xmp.Agent,
   131  			CreateDate:  xmp.Now(),
   132  			ModifyDate:  xmp.Now(),
   133  			Thumbnails: xmpbase.ThumbnailArray{
   134  				xmpbase.Thumbnail{
   135  					Format: "image/jpeg",
   136  					Width:  10,
   137  					Height: 10,
   138  					Image:  []byte("not a real image"),
   139  				},
   140  			},
   141  		})
   142  		s.AddModel(&dc.DublinCore{
   143  			Format:  "image/jpeg",
   144  			Title:   xmp.NewAltString("demo"),
   145  			Creator: xmp.NewStringList("Alexander Eichhorn"),
   146  			Description: xmp.NewAltString(
   147  				xmp.AltItem{Value: "Go-XMP Demo Model", Lang: "en", IsDefault: true},
   148  				xmp.AltItem{Value: "Go-XMP Beispiel Modell", Lang: "de", IsDefault: false},
   149  			),
   150  		})
   151  		b = marshal(s)
   152  		s.Close()
   153  	}
   154  
   155  	if forig {
   156  		out(b)
   157  	}
   158  
   159  	model := unmarshal(b)
   160  
   161  	if fjson {
   162  		b, err := json.MarshalIndent(model, "", "  ")
   163  		if err != nil {
   164  			fail(err)
   165  		}
   166  		out(b)
   167  	}
   168  
   169  	if fxmp {
   170  		out(marshal(model))
   171  	}
   172  
   173  	if fpath {
   174  		l, err := model.ListPaths()
   175  		if err != nil {
   176  			fail(err)
   177  		}
   178  		for _, v := range l {
   179  			fmt.Printf("%s = %s\n", v.Path.String(), v.Value)
   180  		}
   181  	}
   182  
   183  	model.Close()
   184  
   185  	if debug {
   186  		xmp.DumpStats()
   187  	}
   188  }