mirror of
https://github.com/Tilo-K/ddragon-cdn.git
synced 2026-03-05 13:03:01 +00:00
Added option to change storage location.
This commit is contained in:
33
main.go
33
main.go
@@ -132,8 +132,9 @@ func getVersions() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadDdragon(version string) string {
|
func loadDdragon(version string) string {
|
||||||
|
storageDir := os.Getenv("STORAGE_DIR")
|
||||||
url := fmt.Sprintf("https://ddragon.leagueoflegends.com/cdn/dragontail-%s.tgz", version)
|
url := fmt.Sprintf("https://ddragon.leagueoflegends.com/cdn/dragontail-%s.tgz", version)
|
||||||
filename := "ddragon-" + version + ".tgz"
|
filename := filepath.Join(storageDir, "ddragon-" + version + ".tgz")
|
||||||
|
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
@@ -151,8 +152,9 @@ func loadDdragon(version string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadRankedEmblems() string {
|
func loadRankedEmblems() string {
|
||||||
|
storageDir := os.Getenv("STORAGE_DIR")
|
||||||
url := fmt.Sprintf("https://static.developer.riotgames.com/docs/lol/ranked-emblems.zip")
|
url := fmt.Sprintf("https://static.developer.riotgames.com/docs/lol/ranked-emblems.zip")
|
||||||
filename := "ranked-emblems.zip"
|
filename := filepath.Join(storageDir, "ranked-emblems.zip")
|
||||||
|
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
@@ -170,41 +172,43 @@ func loadRankedEmblems() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentVersion() string {
|
func getCurrentVersion() string {
|
||||||
_, err := os.Stat("current.txt")
|
storageDir := os.Getenv("STORAGE_DIR")
|
||||||
|
_, err := os.Stat(filepath.Join(storageDir, "current.txt"))
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat("data")
|
_, err = os.Stat(filepath.Join(storageDir, "data"))
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadFile("current.txt")
|
data, err := ioutil.ReadFile(filepath.Join(storageDir, "current.txt"))
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
return string(data)
|
return string(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCurrent() {
|
func loadCurrent() {
|
||||||
|
storageDir := os.Getenv("STORAGE_DIR")
|
||||||
versions := getVersions()
|
versions := getVersions()
|
||||||
if versions == nil{
|
if versions == nil{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if getCurrentVersion() != versions[0]{
|
if getCurrentVersion() != versions[0]{
|
||||||
file := loadDdragon(versions[0])
|
file := loadDdragon(versions[0])
|
||||||
ioutil.WriteFile("current.txt", []byte(versions[0]), 0777)
|
ioutil.WriteFile(filepath.Join(storageDir, "current.txt"), []byte(versions[0]), 0777)
|
||||||
|
|
||||||
path, err := tgz.Extract(file)
|
path, err := tgz.Extract(file)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
_, err = os.Stat("data")
|
_, err = os.Stat(filepath.Join(storageDir, "data"))
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
err = os.RemoveAll("data")
|
err = os.RemoveAll(filepath.Join(storageDir, "data"))
|
||||||
}
|
}
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
dest, err := filepath.Abs("./data")
|
dest, err := filepath.Abs(filepath.Join(storageDir, "data"))
|
||||||
checkError(err)
|
checkError(err)
|
||||||
err = os.Rename(path, dest)
|
err = os.Rename(path, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -212,14 +216,14 @@ func loadCurrent() {
|
|||||||
}
|
}
|
||||||
os.RemoveAll(path)
|
os.RemoveAll(path)
|
||||||
|
|
||||||
src, _ := filepath.Abs(filepath.Join("data", versions[0]))
|
src, _ := filepath.Abs(filepath.Join(storageDir, "data", versions[0]))
|
||||||
dst, _ := filepath.Abs(filepath.Join("data", "latest"))
|
dst, _ := filepath.Abs(filepath.Join(storageDir, "data", "latest"))
|
||||||
|
|
||||||
err = os.Rename(src, dst)
|
err = os.Rename(src, dst)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
|
|
||||||
export, _ := filepath.Abs(filepath.Join("data", "ranked-emblems"))
|
export, _ := filepath.Abs(filepath.Join(storageDir, "data", "ranked-emblems"))
|
||||||
err = CopyDirectory("./ranked-emblems", export)
|
err = CopyDirectory(filepath.Join(storageDir, "ranked-emblems"), export)
|
||||||
checkError(err)
|
checkError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,6 +237,7 @@ func cors(fs http.Handler) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
storageDir := os.Getenv("STORAGE_DIR")
|
||||||
loadCurrent()
|
loadCurrent()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -245,7 +250,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fs := http.FileServer(http.Dir("./data"))
|
fs := http.FileServer(http.Dir(filepath.Join(storageDir, "data")))
|
||||||
http.Handle("/", cors(fs))
|
http.Handle("/", cors(fs))
|
||||||
|
|
||||||
log.Print("Listening on :60002...")
|
log.Print("Listening on :60002...")
|
||||||
|
|||||||
Reference in New Issue
Block a user