No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

media.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package caddyhugo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "path"
  9. "github.com/PuerkitoBio/goquery"
  10. "github.com/gohugoio/hugo/hugolib"
  11. "git.stephensearles.com/stephen/caddy-hugo2/media"
  12. )
  13. func (ch *CaddyHugo) ReferencedMedia() map[string]map[*hugolib.Page]struct{} {
  14. found := map[string]map[*hugolib.Page]struct{}{}
  15. for _, page := range ch.HugoSites.Pages() {
  16. r := bytes.NewBufferString(string(page.Render()))
  17. doc, err := goquery.NewDocumentFromReader(r)
  18. if err != nil {
  19. continue
  20. }
  21. doc.Find("img,video").Map(func(i int, s *goquery.Selection) string {
  22. u, ok := s.Attr("src")
  23. if ok {
  24. u = path.Base(u)
  25. if ud, err := url.QueryUnescape(u); err == nil {
  26. u = ud
  27. }
  28. if m := found[u]; m == nil {
  29. found[u] = make(map[*hugolib.Page]struct{})
  30. }
  31. found[u][page] = struct{}{}
  32. }
  33. return ""
  34. })
  35. }
  36. return found
  37. }
  38. func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, error) {
  39. if ch.Media == nil {
  40. http.NotFound(w, r)
  41. return 404, nil
  42. }
  43. mr, err := r.MultipartReader()
  44. if err != nil {
  45. http.Error(w, err.Error(), http.StatusBadRequest)
  46. return 400, nil
  47. }
  48. for {
  49. part, err := mr.NextPart()
  50. if err == io.EOF {
  51. break
  52. }
  53. if err != nil {
  54. http.Error(w, err.Error(), http.StatusBadRequest)
  55. return 400, nil
  56. }
  57. name := part.FileName()
  58. if name != "" {
  59. err = ch.Media.ReceiveNewMedia(name, part)
  60. if err != nil {
  61. http.Error(w, err.Error(), http.StatusInternalServerError)
  62. return 500, nil
  63. }
  64. }
  65. }
  66. return 200, nil
  67. }
  68. func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int, error) {
  69. if ch.Media == nil {
  70. http.NotFound(w, r)
  71. return 404, nil
  72. }
  73. referenced := ch.ReferencedMedia()
  74. io.WriteString(w, `<html>
  75. <head><style>
  76. iframe { height: 100%; }
  77. .img { display: inline-block; text-align: center; max-width: 19.9vw; min-height: 120px;}
  78. .img.selected { background-color: lightblue; }
  79. .copy { cursor: pointer; }
  80. @media (max-width: 800px) {
  81. .img {
  82. width: 50%;
  83. max-width: 50%;
  84. }
  85. body {
  86. min-height: 300px;
  87. }
  88. }
  89. </style></head>
  90. <body>
  91. <div style="position: fixed; top: 0; height: 10vh; width: 100%; background-color: white;">
  92. `)
  93. io.WriteString(w, UploadPage("media"))
  94. io.WriteString(w, `</div>
  95. <div style="display: inline-block; width: 100%; height: 10vh;"></div>
  96. `)
  97. if ch.Media != nil {
  98. mm, err := ch.Media.Walk()
  99. if err != nil {
  100. http.Error(w, err.Error(), http.StatusInternalServerError)
  101. return 500, nil
  102. }
  103. for _, m := range media.Set(mm).ByDate() {
  104. size, err := ch.Media.ThumbMax(*m, 100)
  105. if err != nil {
  106. fmt.Fprintf(w, `<div class="img">error rendering %q: %v</div>`, m.Name, err)
  107. continue
  108. }
  109. refs := len(referenced[m.Name])
  110. plural := "s"
  111. if refs == 1 {
  112. plural = ""
  113. }
  114. refLine := fmt.Sprintf("included on %d page%s", refs, plural)
  115. switch m.Type {
  116. case media.TypeImage:
  117. fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q data-filename=%q /><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span><br />%s</div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size), refLine)
  118. case media.TypeVideo:
  119. // TODO: onmouseover sucks for mobile
  120. fmt.Fprintf(w, `<div class="img"><video width=%d height=%d src=%q data-filename=%q onmouseover="this.play()" onmouseout="this.pause();this.currentTime=0;"></video><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span><br />%s</div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size), refLine)
  121. }
  122. }
  123. }
  124. io.WriteString(w, `<script>
  125. document.querySelector('body').onclick = function (evt) {
  126. if (evt.target.tagName === "INPUT" && evt.target.type === "text") {
  127. evt.target.select();
  128. }
  129. if (evt.target.tagName === "SPAN" && evt.target.className === "copy") {
  130. evt.target.previousSibling.select();
  131. document.execCommand("copy");
  132. }
  133. if (evt.target.tagName === "IMG" || evt.target.tagName === "VIDEO") {
  134. var current = document.querySelector(".img.selected");
  135. if (current) {
  136. current.classList = "img";
  137. }
  138. evt.target.parentElement.classList = "img selected";
  139. if (window.parent) {
  140. window.parent.postMessage(evt.target.dataset.filename, location.origin);
  141. }
  142. }
  143. }
  144. document.querySelector('body').onmouseup = function (evt) {
  145. if (evt.target.tagName === "INPUT" && evt.target.type === "text") {
  146. return false;
  147. }
  148. }
  149. </script></body><html>`)
  150. return 200, nil
  151. }
  152. func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, error) {
  153. if ch.Media == nil {
  154. http.NotFound(w, r)
  155. return 404, nil
  156. }
  157. ch.Media.ServeHTTP(w, r)
  158. return 200, nil
  159. }