pub_files.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "encoding/xml"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "github.com/fatih/structs"
  9. "github.com/shurcooL/go-goon"
  10. "gopkg.in/errgo.v1"
  11. "gopkg.in/urfave/cli.v2"
  12. )
  13. type Files struct {
  14. File []FilesMeta `xml:"file"`
  15. }
  16. type FilesMeta struct {
  17. Name string `xml:"name,attr"`
  18. Source string `xml:"source,attr"`
  19. Md5 string `xml:"md5"`
  20. Mtime string `xml:"mtime"`
  21. Size string `xml:"size"`
  22. Crc32 string `xml:"crc32"`
  23. Sha1 string `xml:"sha1"`
  24. Format string `xml:"format"`
  25. Title string `xml:"title"`
  26. Creator string `xml:"creator"`
  27. Album string `xml:"album"`
  28. Track string `xml:"track"`
  29. Length string `xml:"length"`
  30. Height string `xml:"height"`
  31. Width string `xml:"width"`
  32. Rotation string `xml:"rotation"`
  33. Btih string `xml:"btih"`
  34. }
  35. func filesCmd(ctx *cli.Context) error {
  36. files, err := os.Open(ctx.Args().First())
  37. if err != nil {
  38. return err
  39. }
  40. defer files.Close()
  41. fdec := xml.NewDecoder(files)
  42. var fdata Files
  43. err = fdec.Decode(&fdata)
  44. if err != nil {
  45. return err
  46. }
  47. for _, f := range fdata.File {
  48. _, err := os.Stat(f.Name)
  49. if err != nil && os.IsNotExist(err) {
  50. return errgo.Notef(err, "missing file:", f.Name)
  51. }
  52. }
  53. for _, f := range fdata.File {
  54. m := structs.Map(f)
  55. switch f.Source {
  56. case "original":
  57. switch f.Format {
  58. case "224Kbps MP3":
  59. fallthrough
  60. case "VBR MP3":
  61. m["type"] = "audio-mp3"
  62. case "Ogg Vorbis":
  63. m["type"] = "audio-ogg"
  64. case "JPEG":
  65. m["type"] = "meta-image"
  66. default:
  67. m["type"] = f.Format
  68. }
  69. case "metadata":
  70. m["type"] = "meta-data"
  71. default:
  72. m["type"] = "meta-unknown"
  73. m["type-archivedotorg"] = f.Source
  74. // return errgo.Newf("unknown source: %+v", f)
  75. }
  76. blobFile, err := os.Open(f.Name)
  77. if err != nil {
  78. return errgo.Notef(err, "failed to open blob of: %+v", f)
  79. }
  80. blobAdd := exec.Command("sbot", "blobs.add")
  81. blobAdd.Stdin = blobFile
  82. key, err := blobAdd.CombinedOutput()
  83. if err != nil {
  84. return errgo.Notef(err, "'sbot blobs.add' failed on of: %+v", f)
  85. }
  86. blobFile.Close()
  87. m["link"] = strings.TrimSpace(string(key))
  88. if r := ctx.String("root"); r != "" {
  89. m["root"] = r
  90. m["branch"] = r
  91. }
  92. // remove empty fields
  93. for k, v := range m {
  94. if vv, ok := v.(string); ok {
  95. if vv == "" {
  96. delete(m, k)
  97. }
  98. }
  99. }
  100. var reply map[string]interface{}
  101. err = client.Call("publish", m, &reply)
  102. if err != nil {
  103. goon.Dump(m)
  104. return errgo.Notef(err, "publish call failed on %+v", f)
  105. }
  106. log.Println("published..!")
  107. goon.Dump(reply)
  108. }
  109. return nil
  110. }
  111. func check(err error) {
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. }