/
rwImageInfo.go
executable file
·64 lines (54 loc) · 1.62 KB
/
rwImageInfo.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package lib
import (
"io/ioutil"
"log"
"os"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ImageInfoChan - channel to send signal to write current state of imageInfo map into file
var ImageInfoChan = make(chan bool, 1000)
// WriteImageInfoToFile function writes current state of imageInfo map into file
func WriteImageInfoToFile(imageInfoChan chan bool) {
for {
_, ok := <-imageInfoChan
if !ok {
log.Println("ImageInfoChan closed:: writing to file storage is corrupted")
return
}
ImageInfo.Lock()
tempJSONByte, marshalErr := json.Marshal(ImageInfo.AlbumMap)
ImageInfo.Unlock()
if marshalErr != nil {
log.Println("Error in marshalling ImageInfo: ", marshalErr)
}
fileWriteErr := ioutil.WriteFile("image-db/image-info.json", tempJSONByte, 0755)
if fileWriteErr != nil {
log.Println("Cannot write into image-info.json: ", fileWriteErr)
}
}
}
// ReadImageInfoFromFile function reads the image info on start of the application
func ReadImageInfoFromFile() {
permErr := os.Chmod("image-db", 0777)
if permErr != nil {
log.Println("Error in setting folder permission: ", permErr)
}
_, statErr := os.Stat("image-db/image-info.json")
if os.IsNotExist(statErr) {
ImageInfoChan <- true
log.Println("Writing initial database file")
} else {
byteFile, readErr := ioutil.ReadFile("image-db/image-info.json")
if readErr != nil {
panic(readErr)
}
ImageInfo.Lock()
jsonbinderr := json.Unmarshal(byteFile, &ImageInfo.AlbumMap)
ImageInfo.Unlock()
if jsonbinderr != nil {
panic(jsonbinderr)
}
log.Println("Reading existing database file")
}
}