Andreas Mieke
0a69d4a9f2
Implemented getters for: * Snapchat * Twitter * Instagram Also implemented config and database modules.
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package twitter
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
"git.1750studios.com/AniNite/SocialDragon/config"
|
|
)
|
|
|
|
func MediaNameGenerator(seed string) (string, string) {
|
|
seedBytes := []byte(seed)
|
|
sha256Bytes := sha256.Sum256(seedBytes)
|
|
hash := hex.EncodeToString(sha256Bytes[:])
|
|
folders := config.C.ContentDirectory + "/" + hash[0:2] + "/" + hash[0:4] + "/"
|
|
urls := config.C.ContentWebDirectory + "/" + hash[0:2] + "/" + hash[0:4] + "/"
|
|
if err := os.MkdirAll(folders, 0775); err != nil {
|
|
log.Fatalf("FAT Could not create ContentDirectory, error: %+v", err)
|
|
}
|
|
finalPath := folders + hash
|
|
finalUrl := urls + hash
|
|
return finalPath, finalUrl
|
|
}
|
|
|
|
func DownloadMedia(data io.Reader, path string, video bool) (string, error) {
|
|
if video {
|
|
ext := ".mp4"
|
|
blob, err := ioutil.ReadAll(data)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(blob) == 0 {
|
|
return "", errors.New("Empty response")
|
|
}
|
|
err = ioutil.WriteFile(path+ext, blob, 0644)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ext, nil
|
|
} else {
|
|
ext := ".jpg"
|
|
image, err := imaging.Decode(data)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
err = imaging.Save(image, path+ext)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ext, nil
|
|
}
|
|
}
|