Add folder support

This adds folder support as image source, the folder must be specified
in the config file as "Folder" under group "Folder". Parsed images will
be deleted from the folder!
This commit is contained in:
Andreas Mieke 2017-05-13 23:29:31 +02:00
parent 952b8940c9
commit be67ae464c
4 changed files with 71 additions and 0 deletions

View file

@ -17,6 +17,11 @@ type Config struct {
Snapchat Snapchat
Twitter Twitter
Instagram Instagram
Folder Folder
}
type Folder struct {
Folder string
}
type Instagram struct {

View file

@ -16,6 +16,7 @@ const (
Snapchat Service = iota
Twitter
Instagram
Folder
)
type State uint

61
folder/main.go Normal file
View file

@ -0,0 +1,61 @@
package folder
import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/leonelquinteros/gorand"
"git.1750studios.com/AniNite/SocialDragon/config"
"git.1750studios.com/AniNite/SocialDragon/database"
)
func LoadNewFolders() {
log.Printf("Loading new folders...")
files, _ := ioutil.ReadDir(config.C.Folder.Folder)
for _, f := range files {
uuid, err := gorand.UUID()
if err != nil {
log.Printf("Could not generate UUID!")
continue
}
fpath, url := ImageNameGenerator(uuid + f.Name())
err = os.Rename(path.Join(config.C.Folder.Folder, f.Name()), fpath)
if err != nil {
log.Printf("Could not move %s to %s!", path.Join(config.C.Folder.Folder, f.Name()), fpath)
continue
}
var US database.User
database.Db.FirstOrCreate(&US, database.User{Service: database.Folder})
var IT database.Item
IT.UserID = US.ID
IT.IsVideo = false
IT.OriginalID = f.Name()
IT.Path = url
IT.Service = database.Folder
IT.State = database.Inbox
database.Db.Create(&IT)
os.Remove(path.Join(config.C.Folder.Folder, f.Name()))
log.Printf("Found picture %s", f.Name())
}
}
func ImageNameGenerator(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)
}
ext := strings.Split(seed, ".")
finalPath := folders + hash + "." + ext[len(ext)-1]
finalUrl := urls + hash + "." + ext[len(ext)-1]
return finalPath, finalUrl
}

View file

@ -10,6 +10,7 @@ import (
"git.1750studios.com/AniNite/SocialDragon/config"
"git.1750studios.com/AniNite/SocialDragon/database"
"git.1750studios.com/AniNite/SocialDragon/folder"
"git.1750studios.com/AniNite/SocialDragon/instagram"
"git.1750studios.com/AniNite/SocialDragon/snapchat"
"git.1750studios.com/AniNite/SocialDragon/twitter"
@ -26,6 +27,9 @@ func main() {
if config.C.Instagram.Tag != "" {
c.AddFunc("@every 30s", instagram.LoadNewInstas)
}
if config.C.Folder.Folder != "" {
c.AddFunc("@every 30s", folder.LoadNewFolders)
}
c.Start()
if len(config.C.Twitter.Filter) != 0 {