/* meta4a -- Metalink 4.0 creator Copyright (C) 2021-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Metalink 4.0 creator package main import ( "bufio" "encoding/xml" "flag" "io" "log" "os" "path" "strings" "time" "go.stargrave.org/meta4ra" ) func main() { fn := flag.String("fn", "", "Filename") mtime := flag.String("mtime", "", "Take that file's mtime as a Published date") desc := flag.String("desc", "", "Description") sigPGP := flag.String("sig-pgp", "", "Path to OpenPGP .asc signature file") sigSSH := flag.String("sig-ssh", "", "Path to OpenSSH .sig signature file") hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s") torrent := flag.String("torrent", "", "Torrent URL") log.SetFlags(log.Lshortfile) flag.Parse() if *fn == "" { log.Fatalln("empty -fn") } urls := make([]meta4ra.URL, 0, len(flag.Args())) for _, u := range flag.Args() { urls = append(urls, meta4ra.URL{URL: u}) } h := meta4ra.NewHasher(*hashes) h.Start() br := bufio.NewReaderSize(os.Stdin, 1<<20) buf := make([]byte, 1<<20) size, err := io.CopyBuffer(h, br, buf) if err != nil { log.Fatalln(err) } f := meta4ra.File{ Name: path.Base(*fn), Description: *desc, Size: uint64(size), URLs: urls, Hashes: h.Sums(), } if *sigPGP != "" { sigData, err := os.ReadFile(*sigPGP) if err != nil { log.Fatalln(err) } f.Signature = append(f.Signature, meta4ra.Signature{ MediaType: meta4ra.SigMediaTypePGP, Signature: "\n" + string(sigData), }) } if *sigSSH != "" { sigData, err := os.ReadFile(*sigSSH) if err != nil { log.Fatalln(err) } f.Signature = append(f.Signature, meta4ra.Signature{ MediaType: meta4ra.SigMediaTypeSSH, Signature: "\n" + string(sigData), }) } if *torrent != "" { f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}} } var published time.Time if *mtime == "" { published = time.Now() } else { fi, err := os.Stat(*mtime) if err != nil { log.Fatalln(err) } published = fi.ModTime() } published = published.UTC().Truncate(time.Second) m := meta4ra.Metalink{ Files: []meta4ra.File{f}, Generator: meta4ra.Generator, Published: published, } out, err := xml.MarshalIndent(&m, "", " ") if err != nil { log.Fatalln(err) } os.Stdout.Write([]byte(xml.Header)) os.Stdout.Write(out) os.Stdout.Write([]byte("\n")) }