folder.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package my_util
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. )
  10. // --------------------------------------------------------------
  11. // Debug
  12. // --------------------------------------------------------------
  13. // GetRootDebugFolder 在程序的根目录新建,调试用文件夹
  14. func GetRootDebugFolder() (string, error) {
  15. if global_value.DefDebugFolder == "" {
  16. nowProcessRoot, _ := os.Getwd()
  17. nowProcessRoot = filepath.Join(nowProcessRoot, DebugFolder)
  18. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  19. if err != nil {
  20. return "", err
  21. }
  22. global_value.DefDebugFolder = nowProcessRoot
  23. return nowProcessRoot, err
  24. }
  25. return global_value.DefDebugFolder, nil
  26. }
  27. // GetDebugFolderByName 根据传入的 strings (["aa", "bb"]) 组成 DebugFolder/aa/bb 这样的路径
  28. func GetDebugFolderByName(names []string) (string, error) {
  29. rootPath, err := GetRootDebugFolder()
  30. if err != nil {
  31. return "", err
  32. }
  33. tmpFolderFullPath := rootPath
  34. for _, name := range names {
  35. tmpFolderFullPath = filepath.Join(tmpFolderFullPath, name)
  36. }
  37. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  38. if err != nil {
  39. return "", err
  40. }
  41. return tmpFolderFullPath, nil
  42. }
  43. // CopyFiles2DebugFolder 把文件放入到 Debug 文件夹中,新建 desFolderName 文件夹
  44. func CopyFiles2DebugFolder(names []string, subFiles []string) error {
  45. debugFolderByName, err := GetDebugFolderByName(names)
  46. if err != nil {
  47. return err
  48. }
  49. // 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
  50. for _, subFile := range subFiles {
  51. newFn := filepath.Join(debugFolderByName, filepath.Base(subFile))
  52. err = CopyFile(subFile, newFn)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }
  59. // --------------------------------------------------------------
  60. // Tmp
  61. // --------------------------------------------------------------
  62. // GetRootTmpFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
  63. func GetRootTmpFolder() (string, error) {
  64. if global_value.DefTmpFolder == "" {
  65. nowProcessRoot, _ := os.Getwd()
  66. nowProcessRoot = filepath.Join(nowProcessRoot, TmpFolder)
  67. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  68. if err != nil {
  69. return "", err
  70. }
  71. global_value.DefTmpFolder = nowProcessRoot
  72. return nowProcessRoot, err
  73. }
  74. return global_value.DefTmpFolder, nil
  75. }
  76. // GetTmpFolderByName 获取缓存的文件夹,没有则新建
  77. func GetTmpFolderByName(folderName string) (string, error) {
  78. rootPath, err := GetRootTmpFolder()
  79. if err != nil {
  80. return "", err
  81. }
  82. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  83. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  84. if err != nil {
  85. return "", err
  86. }
  87. return tmpFolderFullPath, nil
  88. }
  89. // ClearTmpFolderByName 清理指定的缓存文件夹
  90. func ClearTmpFolderByName(folderName string) error {
  91. nowTmpFolder, err := GetTmpFolderByName(folderName)
  92. if err != nil {
  93. return err
  94. }
  95. return ClearFolder(nowTmpFolder)
  96. }
  97. // ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
  98. func ClearRootTmpFolder() error {
  99. nowTmpFolder, err := GetRootTmpFolder()
  100. if err != nil {
  101. return err
  102. }
  103. pathSep := string(os.PathSeparator)
  104. files, err := os.ReadDir(nowTmpFolder)
  105. if err != nil {
  106. return err
  107. }
  108. for _, curFile := range files {
  109. fullPath := nowTmpFolder + pathSep + curFile.Name()
  110. if curFile.IsDir() {
  111. err = os.RemoveAll(fullPath)
  112. if err != nil {
  113. return err
  114. }
  115. } else {
  116. // 这里就是文件了
  117. err = os.Remove(fullPath)
  118. if err != nil {
  119. return err
  120. }
  121. }
  122. }
  123. return nil
  124. }
  125. // --------------------------------------------------------------
  126. // Sub Fix Cache
  127. // --------------------------------------------------------------
  128. // GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
  129. func GetRootSubFixCacheFolder() (string, error) {
  130. if global_value.DefSubFixCacheFolder == "" {
  131. nowProcessRoot, _ := os.Getwd()
  132. nowProcessRoot = filepath.Join(nowProcessRoot, SubFixCacheFolder)
  133. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  134. if err != nil {
  135. return "", err
  136. }
  137. global_value.DefSubFixCacheFolder = nowProcessRoot
  138. return nowProcessRoot, err
  139. }
  140. return global_value.DefSubFixCacheFolder, nil
  141. }
  142. // GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
  143. func GetSubFixCacheFolderByName(folderName string) (string, error) {
  144. rootPath, err := GetRootSubFixCacheFolder()
  145. if err != nil {
  146. return "", err
  147. }
  148. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  149. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  150. if err != nil {
  151. return "", err
  152. }
  153. return tmpFolderFullPath, nil
  154. }
  155. // --------------------------------------------------------------
  156. // Common
  157. // --------------------------------------------------------------
  158. // ClearFolder 清空文件夹
  159. func ClearFolder(folderFullPath string) error {
  160. pathSep := string(os.PathSeparator)
  161. files, err := os.ReadDir(folderFullPath)
  162. if err != nil {
  163. return err
  164. }
  165. for _, curFile := range files {
  166. fullPath := folderFullPath + pathSep + curFile.Name()
  167. if curFile.IsDir() {
  168. err = os.RemoveAll(fullPath)
  169. if err != nil {
  170. return err
  171. }
  172. } else {
  173. // 这里就是文件了
  174. err = os.Remove(fullPath)
  175. if err != nil {
  176. return err
  177. }
  178. }
  179. }
  180. return nil
  181. }
  182. // ClearFolderEx 清空文件夹,文件夹名称有特殊之处,Hour-min-Nanosecond 的命名方式
  183. // 如果调用的时候,已存在的文件夹的时间 min < 5 那么则清理
  184. func ClearFolderEx(folderFullPath string, overtime int) error {
  185. _, hour, minute, _ := GetNowTimeString()
  186. pathSep := string(os.PathSeparator)
  187. files, err := os.ReadDir(folderFullPath)
  188. if err != nil {
  189. return err
  190. }
  191. for _, curFile := range files {
  192. fullPath := folderFullPath + pathSep + curFile.Name()
  193. if curFile.IsDir() {
  194. parts := strings.Split(curFile.Name(), "-")
  195. if len(parts) == 3 {
  196. // 基本是符合了,倒是还是需要额外的判断是否时间超过了
  197. tmpHourStr := parts[0]
  198. tmpMinuteStr := parts[1]
  199. tmpHour, err := strconv.Atoi(tmpHourStr)
  200. if err != nil {
  201. // 如果不符合命名格式,直接删除
  202. err = os.RemoveAll(fullPath)
  203. if err != nil {
  204. return err
  205. }
  206. continue
  207. }
  208. tmpMinute, err := strconv.Atoi(tmpMinuteStr)
  209. if err != nil {
  210. // 如果不符合命名格式,直接删除
  211. err = os.RemoveAll(fullPath)
  212. if err != nil {
  213. return err
  214. }
  215. continue
  216. }
  217. // 判断时间
  218. if tmpHour != hour {
  219. // 如果不符合命名格式,直接删除
  220. err = os.RemoveAll(fullPath)
  221. if err != nil {
  222. return err
  223. }
  224. continue
  225. }
  226. // 超过 5 min
  227. if minute-overtime > tmpMinute {
  228. // 如果不符合命名格式,直接删除
  229. err = os.RemoveAll(fullPath)
  230. if err != nil {
  231. return err
  232. }
  233. continue
  234. }
  235. } else {
  236. // 如果不符合命名格式,直接删除
  237. err = os.RemoveAll(fullPath)
  238. if err != nil {
  239. return err
  240. }
  241. }
  242. } else {
  243. // 这里就是文件了
  244. err = os.Remove(fullPath)
  245. if err != nil {
  246. return err
  247. }
  248. }
  249. }
  250. return nil
  251. }
  252. // GetConfigRootDirFPath 获取 Config 的根目录,不同系统不一样
  253. func GetConfigRootDirFPath() string {
  254. nowConfigFPath := ""
  255. sysType := runtime.GOOS
  256. if sysType == "linux" {
  257. nowConfigFPath = configDirRootFPathLinux
  258. } else if sysType == "windows" {
  259. nowConfigFPath = configDirRootFPathWindows
  260. } else if sysType == "darwin" {
  261. home, err := os.UserHomeDir()
  262. if err != nil {
  263. panic("GetConfigRootDirFPath darwin get UserHomeDir, Error:" + err.Error())
  264. }
  265. nowConfigFPath = home + configDirRootFPathDarwin
  266. } else {
  267. panic("GetConfigRootDirFPath can't matched OSType: " + sysType + " ,You Should Implement It Yourself")
  268. }
  269. return nowConfigFPath
  270. }
  271. const (
  272. DebugFolder = "CSF-DebugThings" // 调试相关的文件夹
  273. TmpFolder = "CSF-TmpThings" // 临时缓存的文件夹
  274. SubFixCacheFolder = "CSF-SubFixCache" // 字幕时间校正的缓存文件夹,一般可以不清理
  275. )
  276. const (
  277. configDirRootFPathWindows = "./" // Windows 就是在当前的程序目录
  278. configDirRootFPathLinux = "/config/" // Linux 是在 /config 下
  279. configDirRootFPathDarwin = "/.config/chinesesubfinder/" // Darwin 是在 os.UserHomeDir()/.config/chinesesubfinder/ 下
  280. )