| 20 | ) |
| 21 | |
| 22 | func CopyFile(src, dst string, withName bool) error { |
| 23 | source, err := os.Open(src) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | defer source.Close() |
| 28 | |
| 29 | if path.Base(src) != path.Base(dst) && !withName { |
| 30 | dst = path.Join(dst, path.Base(src)) |
| 31 | } |
| 32 | if _, err := os.Stat(path.Dir(dst)); err != nil { |
| 33 | if os.IsNotExist(err) { |
| 34 | _ = os.MkdirAll(path.Dir(dst), os.ModePerm) |
| 35 | } |
| 36 | } |
| 37 | target, err := os.OpenFile(dst+"_temp", os.O_RDWR|os.O_CREATE|os.O_TRUNC, constant.FilePerm) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | defer target.Close() |
| 42 | |
| 43 | if _, err = io.Copy(target, source); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | if err = os.Rename(dst+"_temp", dst); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func CopyItem(isDir, withName bool, src, dst string) error { |
| 53 | if path.Base(src) != path.Base(dst) && !withName { |