diff --git a/config/config.go b/config/config.go index a09c56b..4204a2c 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,11 @@ type Config struct { Snapchat Snapchat Twitter Twitter Instagram Instagram + Folder Folder +} + +type Folder struct { + Folder string } type Instagram struct { diff --git a/database/main.go b/database/main.go index d3e727a..d2796d5 100644 --- a/database/main.go +++ b/database/main.go @@ -16,6 +16,7 @@ const ( Snapchat Service = iota Twitter Instagram + Folder ) type State uint diff --git a/folder/main.go b/folder/main.go new file mode 100644 index 0000000..362679b --- /dev/null +++ b/folder/main.go @@ -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 +} diff --git a/socialdragon/main.go b/socialdragon/main.go index 1b16304..1e345ba 100644 --- a/socialdragon/main.go +++ b/socialdragon/main.go @@ -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 {