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
34 "go.stargrave.org/meta4ra"
38 file := flag.String("file", "", "Path to file")
39 mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
40 desc := flag.String("desc", "", "Description")
41 sig := flag.String("sig", "", "Path to signature file")
42 torrent := flag.String("torrent", "", "Torrent URL")
43 log.SetFlags(log.Lshortfile)
45 urls := make([]meta4ra.URL, 0, len(flag.Args()))
46 for _, u := range flag.Args() {
47 urls = append(urls, meta4ra.URL{URL: u})
49 fd, err := os.Open(*file)
57 sha256Hasher := sha256.New()
58 sha512Hasher := sha512.New()
60 io.MultiWriter(sha256Hasher, sha512Hasher),
67 Name: filepath.Base(*file),
69 Size: uint64(fi.Size()),
71 Hashes: []meta4ra.Hash{
72 {Type: meta4ra.HashSHA256, Hash: hex.EncodeToString(sha256Hasher.Sum(nil))},
73 {Type: meta4ra.HashSHA512, Hash: hex.EncodeToString(sha512Hasher.Sum(nil))},
77 sigData, err := os.ReadFile(*sig)
81 f.Signature = &meta4ra.Signature{
82 MediaType: meta4ra.GPGSigMediaType,
83 Signature: "\n" + string(sigData),
87 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
89 var published time.Time
91 published = time.Now()
93 fi, err := os.Stat(*mtime)
97 published = fi.ModTime()
99 published = published.UTC().Truncate(time.Second)
100 m := meta4ra.Metalink{
101 Files: []meta4ra.File{f},
102 Generator: meta4ra.Generator,
103 Published: published,
105 out, err := xml.MarshalIndent(&m, "", " ")
109 os.Stdout.Write([]byte(xml.Header))
111 os.Stdout.Write([]byte("\n"))