2 meta4a -- Metalink 4.0 creator
3 Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Metalink 4.0 creator
35 "go.stargrave.org/meta4ra"
39 file := flag.String("file", "", "Path to file")
40 mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
41 desc := flag.String("desc", "", "Description")
42 sig := flag.String("sig", "", "Path to signature file")
43 torrent := flag.String("torrent", "", "Torrent URL")
44 log.SetFlags(log.Lshortfile)
46 urls := make([]meta4ra.URL, 0, len(flag.Args()))
47 for _, u := range flag.Args() {
48 urls = append(urls, meta4ra.URL{URL: u})
50 fd, err := os.Open(*file)
58 sha256Hasher := sha256.New()
59 sha512Hasher := sha512.New()
61 io.MultiWriter(sha256Hasher, sha512Hasher),
68 Name: filepath.Base(*file),
70 Size: uint64(fi.Size()),
72 Hashes: []meta4ra.Hash{
73 {Type: meta4ra.HashSHA256, Hash: hex.EncodeToString(sha256Hasher.Sum(nil))},
74 {Type: meta4ra.HashSHA512, Hash: hex.EncodeToString(sha512Hasher.Sum(nil))},
78 sigData, err := ioutil.ReadFile(*sig)
82 f.Signature = &meta4ra.Signature{
83 MediaType: meta4ra.GPGSigMediaType,
84 Signature: "\n" + string(sigData),
88 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
90 var published time.Time
92 published = time.Now()
94 fi, err := os.Stat(*mtime)
98 published = fi.ModTime()
100 published = published.UTC().Truncate(time.Second)
101 m := meta4ra.Metalink{
102 Files: []meta4ra.File{f},
103 Generator: meta4ra.Generator,
104 Published: published,
106 out, err := xml.MarshalIndent(&m, "", " ")
110 os.Stdout.Write([]byte(xml.Header))
112 os.Stdout.Write([]byte("\n"))