Bufferize file download (to avoid OOM)
This commit is contained in:
parent
499346e611
commit
992dbfc67d
2 changed files with 25 additions and 20 deletions
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -34,39 +34,42 @@ func createExerciceFile(theme fic.Theme, exercice fic.Exercice, args []string, b
|
||||||
|
|
||||||
if _, err := os.Stat(pathname); os.IsNotExist(err) {
|
if _, err := os.Stat(pathname); os.IsNotExist(err) {
|
||||||
log.Println("Import file from Cloud:", uf.URI, "=>", pathname)
|
log.Println("Import file from Cloud:", uf.URI, "=>", pathname)
|
||||||
if body, err := getCloudFile(uf.URI); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
if err := os.MkdirAll(path.Dir(pathname), 0777); err != nil {
|
if err := os.MkdirAll(path.Dir(pathname), 0777); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
} else if err := getCloudFile(uf.URI, pathname); err != nil {
|
||||||
|
|
||||||
if err := ioutil.WriteFile(pathname, body, 0777); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return exercice.ImportFile(pathname, uf.URI)
|
return exercice.ImportFile(pathname, uf.URI)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCloudFile(pathname string) ([]byte, error) {
|
func getCloudFile(pathname string, dest string) (error) {
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil {
|
if req, err := http.NewRequest("GET", CloudDAVBase+pathname, nil); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
} else {
|
} else {
|
||||||
req.SetBasicAuth(CloudUsername, CloudPassword)
|
req.SetBasicAuth(CloudUsername, CloudPassword)
|
||||||
if resp, err := client.Do(req); err != nil {
|
if resp, err := client.Do(req); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
} else {
|
} else {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, errors.New(resp.Status)
|
if fd, err := os.Create(dest); err != nil {
|
||||||
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
|
return err
|
||||||
return nil, err
|
|
||||||
} else {
|
} else {
|
||||||
return body, nil
|
defer fd.Close();
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return errors.New(resp.Status)
|
||||||
|
} else {
|
||||||
|
writer := bufio.NewWriter(fd)
|
||||||
|
reader := bufio.NewReader(resp.Body)
|
||||||
|
reader.WriteTo(writer)
|
||||||
|
writer.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package fic
|
package fic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -51,8 +52,9 @@ func (e Exercice) ImportFile(filePath string, origin string) (EFile, error) {
|
||||||
} else {
|
} else {
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(fd)
|
||||||
hash := sha1.New()
|
hash := sha1.New()
|
||||||
if _, err := io.Copy(hash, fd); err != nil {
|
if _, err := io.Copy(hash, reader); err != nil {
|
||||||
return EFile{}, err
|
return EFile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue