file.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package file
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "mime/multipart"
  8. "os"
  9. "path"
  10. path2 "path"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. )
  15. // GetSize get the file size
  16. func GetSize(f multipart.File) (int, error) {
  17. content, err := ioutil.ReadAll(f)
  18. return len(content), err
  19. }
  20. // GetExt get the file ext
  21. func GetExt(fileName string) string {
  22. return path.Ext(fileName)
  23. }
  24. // CheckNotExist check if the file exists
  25. func CheckNotExist(src string) bool {
  26. _, err := os.Stat(src)
  27. return os.IsNotExist(err)
  28. }
  29. // CheckPermission check if the file has permission
  30. func CheckPermission(src string) bool {
  31. _, err := os.Stat(src)
  32. return os.IsPermission(err)
  33. }
  34. // IsNotExistMkDir create a directory if it does not exist
  35. func IsNotExistMkDir(src string) error {
  36. if notExist := CheckNotExist(src); notExist {
  37. if err := MkDir(src); err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. // MkDir create a directory
  44. func MkDir(src string) error {
  45. err := os.MkdirAll(src, os.ModePerm)
  46. if err != nil {
  47. return err
  48. }
  49. os.Chmod(src, 0777)
  50. return nil
  51. }
  52. // RMDir remove a directory
  53. func RMDir(src string) error {
  54. err := os.RemoveAll(src)
  55. if err != nil {
  56. return err
  57. }
  58. os.Remove(src)
  59. return nil
  60. }
  61. // Open a file according to a specific mode
  62. func Open(name string, flag int, perm os.FileMode) (*os.File, error) {
  63. f, err := os.OpenFile(name, flag, perm)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return f, nil
  68. }
  69. // MustOpen maximize trying to open the file
  70. func MustOpen(fileName, filePath string) (*os.File, error) {
  71. //dir, err := os.Getwd()
  72. //if err != nil {
  73. // return nil, fmt.Errorf("os.Getwd err: %v", err)
  74. //}
  75. src := filePath
  76. perm := CheckPermission(src)
  77. if perm == true {
  78. return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
  79. }
  80. err := IsNotExistMkDir(src)
  81. if err != nil {
  82. return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
  83. }
  84. f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
  85. if err != nil {
  86. return nil, fmt.Errorf("Fail to OpenFile :%v", err)
  87. }
  88. return f, nil
  89. }
  90. // 判断所给路径文件/文件夹是否存在
  91. func Exists(path string) bool {
  92. _, err := os.Stat(path) //os.Stat获取文件信息
  93. if err != nil {
  94. if os.IsExist(err) {
  95. return true
  96. }
  97. return false
  98. }
  99. return true
  100. }
  101. // 判断所给路径是否为文件夹
  102. func IsDir(path string) bool {
  103. s, err := os.Stat(path)
  104. if err != nil {
  105. return false
  106. }
  107. return s.IsDir()
  108. }
  109. // 判断所给路径是否为文件
  110. func IsFile(path string) bool {
  111. return !IsDir(path)
  112. }
  113. func CreateFile(path string) error {
  114. file, err := os.Create(path)
  115. if err != nil {
  116. return err
  117. }
  118. defer file.Close()
  119. return nil
  120. }
  121. func CreateFileAndWriteContent(path string, content string) error {
  122. file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
  123. if err != nil {
  124. return err
  125. }
  126. defer file.Close()
  127. write := bufio.NewWriter(file)
  128. write.WriteString(content)
  129. write.Flush()
  130. return nil
  131. }
  132. // IsNotExistMkDir create a directory if it does not exist
  133. func IsNotExistCreateFile(src string) error {
  134. if notExist := CheckNotExist(src); notExist == true {
  135. if err := CreateFile(src); err != nil {
  136. return err
  137. }
  138. }
  139. return nil
  140. }
  141. func ReadFullFile(path string) []byte {
  142. file, err := os.Open(path)
  143. if err != nil {
  144. return []byte("")
  145. }
  146. defer file.Close()
  147. content, err := ioutil.ReadAll(file)
  148. if err != nil {
  149. return []byte("")
  150. }
  151. return content
  152. }
  153. // File copies a single file from src to dst
  154. func CopyFile(src, dst string) error {
  155. var err error
  156. var srcfd *os.File
  157. var dstfd *os.File
  158. var srcinfo os.FileInfo
  159. lastPath := src[strings.LastIndex(src, "/")+1:]
  160. if !strings.HasSuffix(dst, "/") {
  161. dst += "/"
  162. }
  163. dstPath := dst
  164. dst += lastPath
  165. for i := 0; Exists(dst); i++ {
  166. name := strings.Split(lastPath, ".")
  167. nameIndex := 0
  168. if len(name) > 2 {
  169. nameIndex = len(name) - 2
  170. }
  171. name[nameIndex] = name[nameIndex] + "(Copy)"
  172. dst = dstPath
  173. for _, v := range name {
  174. dst += v + "."
  175. }
  176. dst = strings.TrimSuffix(dst, ".")
  177. }
  178. if srcfd, err = os.Open(src); err != nil {
  179. return err
  180. }
  181. defer srcfd.Close()
  182. if dstfd, err = os.Create(dst); err != nil {
  183. return err
  184. }
  185. defer dstfd.Close()
  186. if _, err = io.Copy(dstfd, srcfd); err != nil {
  187. return err
  188. }
  189. if srcinfo, err = os.Stat(src); err != nil {
  190. return err
  191. }
  192. return os.Chmod(dst, srcinfo.Mode())
  193. }
  194. //Check for duplicate file names
  195. func GetNoDuplicateFileName(fullPath string) string {
  196. path, fileName := filepath.Split(fullPath)
  197. fileSuffix := path2.Ext(fileName)
  198. filenameOnly := strings.TrimSuffix(fileName, fileSuffix)
  199. for i := 0; Exists(fullPath); i++ {
  200. fullPath = path2.Join(path, filenameOnly+"("+strconv.Itoa(i+1)+")"+fileSuffix)
  201. }
  202. return fullPath
  203. }
  204. // Dir copies a whole directory recursively
  205. func CopyDir(src string, dst string) error {
  206. var err error
  207. var fds []os.FileInfo
  208. var srcinfo os.FileInfo
  209. if srcinfo, err = os.Stat(src); err != nil {
  210. return err
  211. }
  212. if !srcinfo.IsDir() {
  213. if err = CopyFile(src, dst); err != nil {
  214. fmt.Println(err)
  215. }
  216. return nil
  217. }
  218. dstPath := dst
  219. lastPath := src[strings.LastIndex(src, "/")+1:]
  220. dst += "/" + lastPath
  221. for i := 0; Exists(dst); i++ {
  222. dst = dstPath + "/" + lastPath + strconv.Itoa(i+1)
  223. }
  224. if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
  225. return err
  226. }
  227. if fds, err = ioutil.ReadDir(src); err != nil {
  228. return err
  229. }
  230. for _, fd := range fds {
  231. srcfp := path.Join(src, fd.Name())
  232. dstfp := dst //path.Join(dst, fd.Name())
  233. if fd.IsDir() {
  234. if err = CopyDir(srcfp, dstfp); err != nil {
  235. fmt.Println(err)
  236. }
  237. } else {
  238. if err = CopyFile(srcfp, dstfp); err != nil {
  239. fmt.Println(err)
  240. }
  241. }
  242. }
  243. return nil
  244. }
  245. func WriteToPath(data []byte, path, name string) error {
  246. fullPath := path
  247. if strings.HasSuffix(path, "/") {
  248. fullPath += name
  249. } else {
  250. fullPath += "/" + name
  251. }
  252. IsNotExistCreateFile(fullPath)
  253. file, err := os.OpenFile(fullPath,
  254. os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
  255. 0666,
  256. )
  257. if err != nil {
  258. return err
  259. }
  260. defer file.Close()
  261. _, err = file.Write(data)
  262. return err
  263. }
  264. //最终拼接
  265. func SpliceFiles(dir, path string, length int, startPoint int) error {
  266. fullPath := path
  267. IsNotExistCreateFile(fullPath)
  268. file, _ := os.OpenFile(fullPath,
  269. os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
  270. 0666,
  271. )
  272. defer file.Close()
  273. bufferedWriter := bufio.NewWriter(file)
  274. for i := 0; i < length+startPoint; i++ {
  275. data, err := ioutil.ReadFile(dir + "/" + strconv.Itoa(i+startPoint))
  276. if err != nil {
  277. return err
  278. }
  279. _, err = bufferedWriter.Write(data)
  280. if err != nil {
  281. return err
  282. }
  283. }
  284. bufferedWriter.Flush()
  285. return nil
  286. }