-
Notifications
You must be signed in to change notification settings - Fork 16
/
file_copy.go
57 lines (49 loc) · 1.57 KB
/
file_copy.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
package gobatch
import (
"context"
"github.com/chararch/gobatch/file"
"io"
)
type fileCopyHandler struct {
filesToMove []file.FileMove
}
func (handler *fileCopyHandler) Handle(execution *StepExecution) BatchError {
for _, fm := range handler.filesToMove {
ffp := &FilePath{fm.FromFileName}
fromFileName, err := ffp.Format(execution)
if err != nil {
return NewBatchError(ErrCodeGeneral, "get real file path:%v err", fm.FromFileName, err)
}
tfp := &FilePath{fm.ToFileName}
toFileName, err := tfp.Format(execution)
if err != nil {
return NewBatchError(ErrCodeGeneral, "get real file path:%v err", fm.ToFileName, err)
}
//open from-file
ffs := fm.FromFileStore
reader, err := ffs.Open(fromFileName, "utf-8")
if err != nil {
return NewBatchError(ErrCodeGeneral, "open from file:%v err", fromFileName, err)
}
//create to-file
tfs := fm.ToFileStore
writer, err := tfs.Create(toFileName, "utf-8")
if err != nil {
if er := reader.Close(); er != nil {
logger.Error(context.Background(), "close file reader:%v error", fromFileName, er)
}
return NewBatchError(ErrCodeGeneral, "open to file:%v err", toFileName, err)
}
_, err = io.Copy(writer, reader)
if er := reader.Close(); er != nil {
logger.Error(context.Background(), "close file writer:%v error", toFileName, er)
}
if er := writer.Close(); er != nil {
logger.Error(context.Background(), "close file writer:%v error", toFileName, er)
}
if err != nil {
return NewBatchError(ErrCodeGeneral, "copy file: %v -> %v error", fromFileName, toFileName, err)
}
}
return nil
}