folder.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. package pkg
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "time"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/get_access_time"
  9. "github.com/sirupsen/logrus"
  10. )
  11. // --------------------------------------------------------------
  12. // Debug
  13. // --------------------------------------------------------------
  14. // GetRootDebugFolder 在程序的根目录新建,调试用文件夹
  15. func GetRootDebugFolder() (string, error) {
  16. nowProcessRoot, err := os.Getwd()
  17. if err != nil {
  18. return "", err
  19. }
  20. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, DebugFolder)
  21. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  22. if err != nil {
  23. return "", err
  24. }
  25. return nowProcessRoot, err
  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. nowProcessRoot, err := os.Getwd()
  65. if err != nil {
  66. return "", err
  67. }
  68. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, TmpFolder)
  69. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  70. if err != nil {
  71. return "", err
  72. }
  73. return nowProcessRoot, err
  74. }
  75. // GetTmpFolderByName 获取缓存的文件夹,没有则新建
  76. func GetTmpFolderByName(folderName string) (string, error) {
  77. rootPath, err := GetRootTmpFolder()
  78. if err != nil {
  79. return "", err
  80. }
  81. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  82. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  83. if err != nil {
  84. return "", err
  85. }
  86. return tmpFolderFullPath, nil
  87. }
  88. // ClearTmpFolderByName 清理指定的缓存文件夹
  89. func ClearTmpFolderByName(folderName string) error {
  90. nowTmpFolder, err := GetTmpFolderByName(folderName)
  91. if err != nil {
  92. return err
  93. }
  94. return ClearFolder(nowTmpFolder)
  95. }
  96. // ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
  97. func ClearRootTmpFolder() error {
  98. nowTmpFolder, err := GetRootTmpFolder()
  99. if err != nil {
  100. return err
  101. }
  102. pathSep := string(os.PathSeparator)
  103. files, err := os.ReadDir(nowTmpFolder)
  104. if err != nil {
  105. return err
  106. }
  107. for _, curFile := range files {
  108. fullPath := nowTmpFolder + pathSep + curFile.Name()
  109. if curFile.IsDir() {
  110. err = os.RemoveAll(fullPath)
  111. if err != nil {
  112. return err
  113. }
  114. } else {
  115. // 这里就是文件了
  116. err = os.Remove(fullPath)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. }
  122. return nil
  123. }
  124. // --------------------------------------------------------------
  125. // Adblock Cache
  126. // --------------------------------------------------------------
  127. // GetPluginRootFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
  128. func GetPluginRootFolder() (string, error) {
  129. nowProcessRoot, err := os.Getwd()
  130. if err != nil {
  131. return "", err
  132. }
  133. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, PluginFolder)
  134. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  135. if err != nil {
  136. return "", err
  137. }
  138. return nowProcessRoot, err
  139. }
  140. // GetPluginFolderByName 获取缓存的文件夹,没有则新建
  141. func GetPluginFolderByName(folderName string) (string, error) {
  142. rootPath, err := GetPluginRootFolder()
  143. if err != nil {
  144. return "", err
  145. }
  146. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  147. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  148. if err != nil {
  149. return "", err
  150. }
  151. return tmpFolderFullPath, nil
  152. }
  153. // ClearPluginFolderByName 清理指定的缓存文件夹
  154. func ClearPluginFolderByName(folderName string) error {
  155. nowTmpFolder, err := GetPluginFolderByName(folderName)
  156. if err != nil {
  157. return err
  158. }
  159. return ClearFolder(nowTmpFolder)
  160. }
  161. // --------------------------------------------------------------
  162. // Rod Cache
  163. // --------------------------------------------------------------
  164. // GetRodTmpRootFolder 在程序的根目录新建,rod 缓存用文件夹
  165. func GetRodTmpRootFolder() (string, error) {
  166. nowProcessRoot, err := os.Getwd()
  167. if err != nil {
  168. return "", err
  169. }
  170. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, RodCacheFolder)
  171. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  172. if err != nil {
  173. return "", err
  174. }
  175. return nowProcessRoot, err
  176. }
  177. // ClearRodTmpRootFolder 清理 rod 缓存文件夹
  178. func ClearRodTmpRootFolder() error {
  179. nowTmpFolder, err := GetRodTmpRootFolder()
  180. if err != nil {
  181. return err
  182. }
  183. pathSep := string(os.PathSeparator)
  184. files, err := os.ReadDir(nowTmpFolder)
  185. if err != nil {
  186. return err
  187. }
  188. for _, curFile := range files {
  189. fullPath := nowTmpFolder + pathSep + curFile.Name()
  190. if curFile.IsDir() {
  191. err = os.RemoveAll(fullPath)
  192. if err != nil {
  193. return err
  194. }
  195. } else {
  196. // 这里就是文件了
  197. err = os.Remove(fullPath)
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. }
  203. return nil
  204. }
  205. // --------------------------------------------------------------
  206. // Sub Fix Cache
  207. // --------------------------------------------------------------
  208. // GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
  209. func GetRootSubFixCacheFolder() (string, error) {
  210. nowProcessRoot, err := os.Getwd()
  211. if err != nil {
  212. return "", err
  213. }
  214. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, SubFixCacheFolder)
  215. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  216. if err != nil {
  217. return "", err
  218. }
  219. return nowProcessRoot, err
  220. }
  221. // GetRootCacheCenterFolder 下载缓存、队列缓存、下载次数缓存的文件夹
  222. func GetRootCacheCenterFolder() (string, error) {
  223. nowProcessRoot, err := os.Getwd()
  224. if err != nil {
  225. return "", err
  226. }
  227. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, CacheCenterFloder)
  228. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  229. if err != nil {
  230. return "", err
  231. }
  232. return nowProcessRoot, err
  233. }
  234. // GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
  235. func GetSubFixCacheFolderByName(folderName string) (string, error) {
  236. rootPath, err := GetRootSubFixCacheFolder()
  237. if err != nil {
  238. return "", err
  239. }
  240. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  241. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  242. if err != nil {
  243. return "", err
  244. }
  245. return tmpFolderFullPath, nil
  246. }
  247. // --------------------------------------------------------------
  248. // Share Sub Cache
  249. // --------------------------------------------------------------
  250. // GetShareSubRootFolder 在程序的根目录新建,字幕共享的缓存根目录,下级还有具体是按发行的时间去划分的子集目录
  251. func GetShareSubRootFolder() (string, error) {
  252. nowProcessRoot, err := os.Getwd()
  253. if err != nil {
  254. return "", err
  255. }
  256. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, ShareSubFileCache)
  257. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  258. if err != nil {
  259. return "", err
  260. }
  261. return nowProcessRoot, err
  262. }
  263. // GetShareFolderByYear 缓存的文件夹以发行的年为一个单位存储
  264. func GetShareFolderByYear(year int) (string, error) {
  265. rootPath, err := GetShareSubRootFolder()
  266. if err != nil {
  267. return "", err
  268. }
  269. tmpFolderFullPath := filepath.Join(rootPath, fmt.Sprintf("%d", year))
  270. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  271. if err != nil {
  272. return "", err
  273. }
  274. return tmpFolderFullPath, nil
  275. }
  276. // ClearShareSubFolderByYear 清理指定的缓存文件夹
  277. func ClearShareSubFolderByYear(year int) error {
  278. nowTmpFolder, err := GetShareFolderByYear(year)
  279. if err != nil {
  280. return err
  281. }
  282. return ClearFolder(nowTmpFolder)
  283. }
  284. // ClearShareSubFolderByYearAndName 清理指定的缓存文件夹
  285. func ClearShareSubFolderByYearAndName(year int, name string) error {
  286. nowTmpFolder, err := GetShareFolderByYear(year)
  287. if err != nil {
  288. return err
  289. }
  290. return ClearFolder(filepath.Join(nowTmpFolder, name))
  291. }
  292. // --------------------------------------------------------------
  293. // Common
  294. // --------------------------------------------------------------
  295. // ClearFolder 清空文件夹
  296. func ClearFolder(folderFullPath string) error {
  297. pathSep := string(os.PathSeparator)
  298. files, err := os.ReadDir(folderFullPath)
  299. if err != nil {
  300. return err
  301. }
  302. for _, curFile := range files {
  303. fullPath := folderFullPath + pathSep + curFile.Name()
  304. if curFile.IsDir() {
  305. err = os.RemoveAll(fullPath)
  306. if err != nil {
  307. return err
  308. }
  309. } else {
  310. // 这里就是文件了
  311. err = os.Remove(fullPath)
  312. if err != nil {
  313. return err
  314. }
  315. }
  316. }
  317. return nil
  318. }
  319. // GetConfigRootDirFPath 获取 Config 的根目录,不同系统不一样
  320. func GetConfigRootDirFPath() string {
  321. nowConfigFPath := ""
  322. sysType := runtime.GOOS
  323. if sysType == "linux" {
  324. if LinuxConfigPathInSelfPath() != "" {
  325. // 自定义路径
  326. nowConfigFPath = LinuxConfigPathInSelfPath()
  327. } else {
  328. // 专用目录,与 Docker 设置一致
  329. nowConfigFPath = configDirRootFPathLinux
  330. }
  331. } else if sysType == "windows" {
  332. if LinuxConfigPathInSelfPath() != "" {
  333. // 自定义路径
  334. nowConfigFPath = LinuxConfigPathInSelfPath()
  335. } else {
  336. nowConfigFPath = configDirRootFPathWindows
  337. }
  338. } else if sysType == "darwin" {
  339. if LinuxConfigPathInSelfPath() != "" {
  340. // 自定义路径
  341. nowConfigFPath = LinuxConfigPathInSelfPath()
  342. } else {
  343. home, err := os.UserHomeDir()
  344. if err != nil {
  345. panic("GetConfigRootDirFPath darwin get UserHomeDir, Error:" + err.Error())
  346. }
  347. nowConfigFPath = home + configDirRootFPathDarwin
  348. }
  349. } else {
  350. panic("GetConfigRootDirFPath can't matched OSType: " + sysType + " ,You Should Implement It Yourself")
  351. }
  352. return nowConfigFPath
  353. }
  354. // ClearIdleSubFixCacheFolder 清理闲置的字幕修正缓存文件夹
  355. func ClearIdleSubFixCacheFolder(l *logrus.Logger, rootSubFixCacheFolder string, outOfDate time.Duration) error {
  356. /*
  357. 从 GetRootSubFixCacheFolder 目录下,遍历第一级目录中的文件夹
  358. 然后每个文件夹中,统计里面最后的访问时间(可能有多个文件),如果超过某个时间范围就标记删除这个文件夹
  359. */
  360. pathSep := string(os.PathSeparator)
  361. files, err := os.ReadDir(rootSubFixCacheFolder)
  362. if err != nil {
  363. return err
  364. }
  365. wait2ScanFolder := make([]string, 0)
  366. for _, curFile := range files {
  367. fullPath := rootSubFixCacheFolder + pathSep + curFile.Name()
  368. if curFile.IsDir() == true {
  369. // 需要关注文件夹
  370. wait2ScanFolder = append(wait2ScanFolder, fullPath)
  371. }
  372. }
  373. wait2DeleteFolder := make([]string, 0)
  374. getAccessTimeEx := get_access_time.GetAccessTimeEx{}
  375. cutOff := time.Now().Add(-outOfDate)
  376. for _, s := range wait2ScanFolder {
  377. files, err = os.ReadDir(s)
  378. if err != nil {
  379. return err
  380. }
  381. maxAccessTime := time.Now()
  382. // 需要统计这个文件夹下的所有文件的 AccessTIme,找出最新(最大的值)的那个时间,再比较
  383. for i, curFile := range files {
  384. fullPath := s + pathSep + curFile.Name()
  385. if curFile.IsDir() == true {
  386. continue
  387. }
  388. // 只需要关注文件
  389. accessTime, err := getAccessTimeEx.GetAccessTime(fullPath)
  390. if err != nil {
  391. return err
  392. }
  393. if i == 0 {
  394. maxAccessTime = accessTime
  395. }
  396. if Time2SecondNumber(accessTime) > Time2SecondNumber(maxAccessTime) {
  397. maxAccessTime = accessTime
  398. }
  399. }
  400. if maxAccessTime.Sub(cutOff) <= 0 {
  401. // 确认可以删除
  402. wait2DeleteFolder = append(wait2DeleteFolder, s)
  403. }
  404. }
  405. // 统一清理过期的文件夹
  406. for _, s := range wait2DeleteFolder {
  407. l.Infoln("Try 2 clear SubFixCache Folder:", s)
  408. err := os.RemoveAll(s)
  409. if err != nil {
  410. return err
  411. }
  412. }
  413. return nil
  414. }
  415. // 缓存文件的位置信息,都是在程序的根目录下的 cache 中
  416. const (
  417. cacheRootFolderName = "cache" // 缓存文件夹总名称
  418. TmpFolder = "tmp" // 临时缓存的文件夹
  419. RodCacheFolder = "rod" // rod 的缓存目录
  420. PluginFolder = "Plugin" // 插件的目录
  421. DebugFolder = "CSF-DebugThings" // 调试相关的文件夹
  422. SubFixCacheFolder = "CSF-SubFixCache" // 字幕时间校正的缓存文件夹,一般可以不清理
  423. ShareSubFileCache = "CSF-ShareSubCache" // 字幕共享的缓存目录,不建议删除
  424. CacheCenterFloder = "CSF-CacheCenter" // 下载缓存、队列缓存、下载次数缓存的文件夹
  425. )
  426. const (
  427. Plugin_Adblock = "adblock"
  428. )
  429. // 配置文件的位置信息,这个会根据系统版本做区分
  430. const (
  431. configDirRootFPathWindows = "." // Windows 就是在当前的程序目录
  432. configDirRootFPathLinux = "/config" // Linux 是在 /config 下
  433. configDirRootFPathDarwin = "/.config/chinesesubfinder" // Darwin 是在 os.UserHomeDir()/.config/chinesesubfinder/ 下
  434. )