]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-create/main.go
meta4-check workability again
[meta4ra.git] / cmd / meta4-create / main.go
1 /*
2 meta4a -- Metalink 4.0 creator
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 // Metalink 4.0 creator
19 package main
20
21 import (
22         "bufio"
23         "encoding/xml"
24         "flag"
25         "io"
26         "log"
27         "os"
28         "path"
29         "strings"
30         "time"
31
32         "go.stargrave.org/meta4ra"
33 )
34
35 func main() {
36         fn := flag.String("fn", "", "Filename")
37         mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
38         desc := flag.String("desc", "", "Description")
39         sig := flag.String("sig", "", "Path to signature file")
40         hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
41         torrent := flag.String("torrent", "", "Torrent URL")
42         log.SetFlags(log.Lshortfile)
43         flag.Parse()
44         if *fn == "" {
45                 log.Fatalln("empty -fn")
46         }
47         urls := make([]meta4ra.URL, 0, len(flag.Args()))
48         for _, u := range flag.Args() {
49                 urls = append(urls, meta4ra.URL{URL: u})
50         }
51         h := meta4ra.NewHasher(*hashes)
52         h.Start()
53         br := bufio.NewReaderSize(os.Stdin, 1<<20)
54         buf := make([]byte, 1<<20)
55         size, err := io.CopyBuffer(h, br, buf)
56         if err != nil {
57                 log.Fatalln(err)
58         }
59         f := meta4ra.File{
60                 Name:        path.Base(*fn),
61                 Description: *desc,
62                 Size:        uint64(size),
63                 URLs:        urls,
64                 Hashes:      h.Sums(),
65         }
66         if *sig != "" {
67                 sigData, err := os.ReadFile(*sig)
68                 if err != nil {
69                         log.Fatalln(err)
70                 }
71                 f.Signature = &meta4ra.Signature{
72                         MediaType: meta4ra.GPGSigMediaType,
73                         Signature: "\n" + string(sigData),
74                 }
75         }
76         if *torrent != "" {
77                 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
78         }
79         var published time.Time
80         if *mtime == "" {
81                 published = time.Now()
82         } else {
83                 fi, err := os.Stat(*mtime)
84                 if err != nil {
85                         log.Fatalln(err)
86                 }
87                 published = fi.ModTime()
88         }
89         published = published.UTC().Truncate(time.Second)
90         m := meta4ra.Metalink{
91                 Files:     []meta4ra.File{f},
92                 Generator: meta4ra.Generator,
93                 Published: published,
94         }
95         out, err := xml.MarshalIndent(&m, "", "  ")
96         if err != nil {
97                 log.Fatalln(err)
98         }
99         os.Stdout.Write([]byte(xml.Header))
100         os.Stdout.Write(out)
101         os.Stdout.Write([]byte("\n"))
102 }