package twitter import ( "crypto/sha256" "encoding/hex" "errors" "io" "io/ioutil" "log" "os" "github.com/disintegration/imaging" "git.1750studios.com/AniNite/SocialDragon/config" ) // MediaNameGenerator generates paths for media 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 } // DownloadMedia downloads media from tweet 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 } 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 }