summaryrefslogtreecommitdiff
path: root/app/controllers/url.go
blob: 516f6a680388527759a0fc4ff6f994cbbfce46ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package controllers

import (
	"crypto/md5"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)

func HashUrl(url string) (string, error) {
	response, err := http.Get(url)
	if err != nil {
		return "Get request failed.", err
	}

	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)
	if er != nil {
		return "Reading body failed.", err
	}

	h := md5.New()
	io.WriteString(h, string(contents))
	hash := fmt.Sprintf("%x", h.Sum(nil))

	return hash, nil
}