-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.go
73 lines (61 loc) · 1.41 KB
/
file.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
65
66
67
68
69
70
71
72
73
// Copyright (c) 2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package utils
import (
"bufio"
"bytes"
"encoding/json"
"io"
"log"
"os"
"github.com/go-openapi/runtime"
"github.com/sirupsen/logrus"
)
func ReadByChunks(filePath string) (*os.File, error) {
buf := make([]byte, 0, 4*1024) // size of chunks
file, errFile := os.Open(filePath)
if errFile != nil {
log.Fatal("unable to open big file")
}
nBytes, nChunks := int64(0), int64(0)
r := bufio.NewReader(file) // avoid eating RAM
for {
n, errRead := r.Read(buf[:cap(buf)])
buf = buf[:n]
if n == 0 {
if errRead == nil {
continue
}
if errRead == io.EOF {
break
}
log.Fatal(errRead)
}
nChunks++
nBytes += int64(len(buf))
// process buf
if errRead != nil && errRead != io.EOF {
log.Fatal(errRead)
}
}
return file, nil
}
func ConvertByteToFile(fileByte io.Writer, writer *bytes.Buffer, fileName string) (runtime.NamedReadCloser, error) {
_, errMarshal := json.Marshal(fileByte)
if errMarshal != nil {
logrus.Fatal(errMarshal)
}
f, errCreate := os.Create(fileName)
if errCreate != nil {
logrus.Fatal(errCreate)
}
if _, err := io.Copy(f, writer); err != nil {
logrus.Fatal(err.Error())
}
file, errOpen := os.Open(fileName)
if errOpen != nil {
logrus.Fatal(errOpen)
}
return file, nil
}