aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-08-02 23:37:49 +0200
committerMichael Muré <batolettre@gmail.com>2018-08-02 23:37:49 +0200
commitd8f89726fec3822d7d1dd42c52f478f37003b534 (patch)
tree567e97ee605ef3a286e42e11535fa6516321e743 /util
parented8f7eca9a8e0d1c448a7cc6240e2b7e357cf354 (diff)
downloadgit-bug-d8f89726fec3822d7d1dd42c52f478f37003b534.tar.gz
implement media hosting in git for comments + API for the webui
Diffstat (limited to 'util')
-rw-r--r--util/hash.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/util/hash.go b/util/hash.go
index 088fd70e..df0a83b8 100644
--- a/util/hash.go
+++ b/util/hash.go
@@ -1,3 +1,43 @@
package util
+import (
+ "fmt"
+ "io"
+)
+
type Hash string
+
+func (h Hash) String() string {
+ return string(h)
+}
+
+func (h *Hash) UnmarshalGQL(v interface{}) error {
+ _, ok := v.(string)
+ if !ok {
+ return fmt.Errorf("labels must be strings")
+ }
+
+ *h = v.(Hash)
+
+ if !h.IsValid() {
+ return fmt.Errorf("invalid hash")
+ }
+
+ return nil
+}
+
+func (h Hash) MarshalGQL(w io.Writer) {
+ w.Write([]byte(`"` + h.String() + `"`))
+}
+
+func (h *Hash) IsValid() bool {
+ if len(*h) != 40 {
+ return false
+ }
+ for _, r := range *h {
+ if (r < 'a' || r > 'z') && (r < '0' || r > '9') {
+ return false
+ }
+ }
+ return true
+}