Set comment

This commit is contained in:
Milan Nikolic
2023-08-21 20:04:05 +02:00
parent f92125643f
commit 54261d8528
2 changed files with 79 additions and 19 deletions

View File

@@ -366,7 +366,7 @@ func (c *Convertor) convertDirectory(dirPath string) error {
if err != nil {
i, err = c.imDecode(file, img)
if err != nil {
return fmt.Errorf("coverDirectory: %w", err)
return fmt.Errorf("convertDirectory: %w", err)
}
}
@@ -764,7 +764,7 @@ func (c *Convertor) archiveList(fileName string) ([]string, error) {
archive, err := unarr.NewArchive(fileName)
if err != nil {
return contents, err
return contents, fmt.Errorf("archiveList: %w", err)
}
defer archive.Close()
@@ -773,16 +773,70 @@ func (c *Convertor) archiveList(fileName string) ([]string, error) {
// archiveComment returns ZIP comment.
func (c *Convertor) archiveComment(fileName string) (string, error) {
r, err := zip.OpenReader(fileName)
zr, err := zip.OpenReader(fileName)
if err != nil {
return "", err
return "", fmt.Errorf("archiveComment: %w", err)
}
return r.Comment, nil
return zr.Comment, nil
}
// archiveSetComment sets ZIP comment.
func (c *Convertor) archiveSetComment(fileName, commentBody string) error {
zr, err := zip.OpenReader(fileName)
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
defer zr.Close()
zf, err := os.CreateTemp(os.TempDir(), "cbc")
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
tmpName := zf.Name()
defer os.Remove(tmpName)
zw := zip.NewWriter(zf)
zw.SetComment(commentBody)
for _, item := range zr.File {
ir, err := item.OpenRaw()
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
it, err := zw.CreateRaw(&item.FileHeader)
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
_, err = io.Copy(it, ir)
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
}
err = zw.Close()
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
err = zf.Close()
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
data, err := os.ReadFile(tmpName)
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
err = os.WriteFile(fileName, data, 0644)
if err != nil {
return fmt.Errorf("archiveSetComment: %w", err)
}
return nil
}
@@ -1303,6 +1357,10 @@ func (c *Convertor) Meta(fileName string, info os.FileInfo) (any, error) {
return comment, nil
} else if c.Opts.CommentBody != "" {
err := c.archiveSetComment(fileName, c.Opts.CommentBody)
if err != nil {
return nil, err
}
}
return nil, nil

View File

@@ -8,7 +8,7 @@ import (
"syscall"
"github.com/gen2brain/cbconvert"
"github.com/schollz/progressbar/v3"
pb "github.com/schollz/progressbar/v3"
flag "github.com/spf13/pflag"
)
@@ -44,26 +44,26 @@ func main() {
os.Exit(1)
}
var bar *progressbar.ProgressBar
var bar *pb.ProgressBar
if opts.Cover || opts.Thumbnail || opts.Meta {
if !opts.Quiet {
bar = progressbar.NewOptions(conv.Nfiles,
progressbar.OptionShowCount(),
progressbar.OptionClearOnFinish(),
progressbar.OptionUseANSICodes(true),
progressbar.OptionSetPredictTime(false),
bar = pb.NewOptions(conv.Nfiles,
pb.OptionShowCount(),
pb.OptionClearOnFinish(),
pb.OptionUseANSICodes(true),
pb.OptionSetPredictTime(false),
)
}
}
conv.OnStart = func() {
if !opts.Quiet {
bar = progressbar.NewOptions(conv.Ncontents,
progressbar.OptionShowCount(),
progressbar.OptionClearOnFinish(),
progressbar.OptionUseANSICodes(true),
progressbar.OptionSetDescription(fmt.Sprintf("Converting %d of %d:", conv.CurrFile, conv.Nfiles)),
progressbar.OptionSetPredictTime(false),
bar = pb.NewOptions(conv.Ncontents,
pb.OptionShowCount(),
pb.OptionClearOnFinish(),
pb.OptionUseANSICodes(true),
pb.OptionSetDescription(fmt.Sprintf("Converting %d of %d:", conv.CurrFile, conv.Nfiles)),
pb.OptionSetPredictTime(false),
)
}
}
@@ -120,6 +120,8 @@ func main() {
os.Exit(1)
}
}
_, _ = fmt.Fprintf(os.Stderr, "\r")
}
// parseFlags parses command line flags
@@ -185,7 +187,7 @@ func parseFlags() (cbconvert.Options, []string) {
meta.SortFlags = false
meta.BoolVar(&opts.Cover, "cover", false, "Print cover name")
meta.BoolVar(&opts.Comment, "comment", false, "Print comment")
meta.StringVar(&opts.CommentBody, "comment-body", "", "Set comment body (file or string)")
meta.StringVar(&opts.CommentBody, "comment-body", "", "Set comment")
convert.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s <command> [<flags>] [file1 dir1 ... fileOrDirN]\n\n", filepath.Base(os.Args[0]))