folder.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. package pkg
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "time"
  8. "github.com/ChineseSubFinder/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, CacheCenterFolder)
  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. // Manual Subtitle Upload
  294. // --------------------------------------------------------------
  295. // GetManualSubUploadCacheFolder 手动上传字幕的缓存文件夹
  296. func GetManualSubUploadCacheFolder() (string, error) {
  297. nowProcessRoot, err := os.Getwd()
  298. if err != nil {
  299. return "", err
  300. }
  301. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, ManualSubUploadCacheFolder)
  302. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  303. if err != nil {
  304. return "", err
  305. }
  306. return nowProcessRoot, err
  307. }
  308. // ClearManualSubUploadCacheFolder 清理手动上传字幕的缓存文件夹
  309. func ClearManualSubUploadCacheFolder() error {
  310. nowTmpFolder, err := GetManualSubUploadCacheFolder()
  311. if err != nil {
  312. return err
  313. }
  314. pathSep := string(os.PathSeparator)
  315. files, err := os.ReadDir(nowTmpFolder)
  316. if err != nil {
  317. return err
  318. }
  319. for _, curFile := range files {
  320. fullPath := nowTmpFolder + pathSep + curFile.Name()
  321. if curFile.IsDir() {
  322. err = os.RemoveAll(fullPath)
  323. if err != nil {
  324. return err
  325. }
  326. } else {
  327. // 这里就是文件了
  328. err = os.Remove(fullPath)
  329. if err != nil {
  330. return err
  331. }
  332. }
  333. }
  334. return nil
  335. }
  336. // --------------------------------------------------------------
  337. // 视频和字幕的预览缓存
  338. // --------------------------------------------------------------
  339. // GetVideoAndSubPreviewCacheFolder 视频和字幕的预览缓存
  340. func GetVideoAndSubPreviewCacheFolder() (string, error) {
  341. nowProcessRoot, err := os.Getwd()
  342. if err != nil {
  343. return "", err
  344. }
  345. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, VideoAndSubPreviewCacheFolder)
  346. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  347. if err != nil {
  348. return "", err
  349. }
  350. return nowProcessRoot, err
  351. }
  352. // ClearVideoAndSubPreviewCacheFolder 清理视频和字幕的预览缓存
  353. func ClearVideoAndSubPreviewCacheFolder() error {
  354. nowTmpFolder, err := GetVideoAndSubPreviewCacheFolder()
  355. if err != nil {
  356. return err
  357. }
  358. pathSep := string(os.PathSeparator)
  359. files, err := os.ReadDir(nowTmpFolder)
  360. if err != nil {
  361. return err
  362. }
  363. for _, curFile := range files {
  364. fullPath := nowTmpFolder + pathSep + curFile.Name()
  365. if curFile.IsDir() {
  366. err = os.RemoveAll(fullPath)
  367. if err != nil {
  368. return err
  369. }
  370. } else {
  371. // 这里就是文件了
  372. err = os.Remove(fullPath)
  373. if err != nil {
  374. return err
  375. }
  376. }
  377. }
  378. return nil
  379. }
  380. // --------------------------------------------------------------
  381. // Common
  382. // --------------------------------------------------------------
  383. // ClearFolder 清空文件夹
  384. func ClearFolder(folderFullPath string) error {
  385. pathSep := string(os.PathSeparator)
  386. files, err := os.ReadDir(folderFullPath)
  387. if err != nil {
  388. return err
  389. }
  390. for _, curFile := range files {
  391. fullPath := folderFullPath + pathSep + curFile.Name()
  392. if curFile.IsDir() {
  393. err = os.RemoveAll(fullPath)
  394. if err != nil {
  395. return err
  396. }
  397. } else {
  398. // 这里就是文件了
  399. err = os.Remove(fullPath)
  400. if err != nil {
  401. return err
  402. }
  403. }
  404. }
  405. return nil
  406. }
  407. // GetConfigRootDirFPath 获取 Config 的根目录,不同系统不一样
  408. func GetConfigRootDirFPath() string {
  409. nowConfigFPath := ""
  410. sysType := runtime.GOOS
  411. if sysType == "linux" {
  412. if LinuxConfigPathInSelfPath() != "" {
  413. // 自定义路径
  414. nowConfigFPath = LinuxConfigPathInSelfPath()
  415. } else {
  416. // 专用目录,与 Docker 设置一致
  417. nowConfigFPath = configDirRootFPathLinux
  418. }
  419. } else if sysType == "windows" {
  420. if LinuxConfigPathInSelfPath() != "" {
  421. // 自定义路径
  422. nowConfigFPath = LinuxConfigPathInSelfPath()
  423. } else {
  424. nowConfigFPath = configDirRootFPathWindows
  425. }
  426. } else if sysType == "darwin" {
  427. if LinuxConfigPathInSelfPath() != "" {
  428. // 自定义路径
  429. nowConfigFPath = LinuxConfigPathInSelfPath()
  430. } else {
  431. home, err := os.UserHomeDir()
  432. if err != nil {
  433. panic("GetConfigRootDirFPath darwin get UserHomeDir, Error:" + err.Error())
  434. }
  435. nowConfigFPath = home + configDirRootFPathDarwin
  436. }
  437. } else {
  438. panic("GetConfigRootDirFPath can't matched OSType: " + sysType + " ,You Should Implement It Yourself")
  439. }
  440. return nowConfigFPath
  441. }
  442. // ClearIdleSubFixCacheFolder 清理闲置的字幕修正缓存文件夹
  443. func ClearIdleSubFixCacheFolder(l *logrus.Logger, rootSubFixCacheFolder string, outOfDate time.Duration) error {
  444. /*
  445. 从 GetRootSubFixCacheFolder 目录下,遍历第一级目录中的文件夹
  446. 然后每个文件夹中,统计里面最后的访问时间(可能有多个文件),如果超过某个时间范围就标记删除这个文件夹
  447. */
  448. pathSep := string(os.PathSeparator)
  449. files, err := os.ReadDir(rootSubFixCacheFolder)
  450. if err != nil {
  451. return err
  452. }
  453. wait2ScanFolder := make([]string, 0)
  454. for _, curFile := range files {
  455. fullPath := rootSubFixCacheFolder + pathSep + curFile.Name()
  456. if curFile.IsDir() == true {
  457. // 需要关注文件夹
  458. wait2ScanFolder = append(wait2ScanFolder, fullPath)
  459. }
  460. }
  461. wait2DeleteFolder := make([]string, 0)
  462. getAccessTimeEx := get_access_time.GetAccessTimeEx{}
  463. cutOff := time.Now().Add(-outOfDate)
  464. for _, s := range wait2ScanFolder {
  465. files, err = os.ReadDir(s)
  466. if err != nil {
  467. return err
  468. }
  469. maxAccessTime := time.Now()
  470. // 需要统计这个文件夹下的所有文件的 AccessTIme,找出最新(最大的值)的那个时间,再比较
  471. for i, curFile := range files {
  472. fullPath := s + pathSep + curFile.Name()
  473. if curFile.IsDir() == true {
  474. continue
  475. }
  476. // 只需要关注文件
  477. accessTime, err := getAccessTimeEx.GetAccessTime(fullPath)
  478. if err != nil {
  479. return err
  480. }
  481. if i == 0 {
  482. maxAccessTime = accessTime
  483. }
  484. if Time2SecondNumber(accessTime) > Time2SecondNumber(maxAccessTime) {
  485. maxAccessTime = accessTime
  486. }
  487. }
  488. if maxAccessTime.Sub(cutOff) <= 0 {
  489. // 确认可以删除
  490. wait2DeleteFolder = append(wait2DeleteFolder, s)
  491. }
  492. }
  493. // 统一清理过期的文件夹
  494. for _, s := range wait2DeleteFolder {
  495. l.Infoln("Try 2 clear SubFixCache Folder:", s)
  496. err := os.RemoveAll(s)
  497. if err != nil {
  498. return err
  499. }
  500. }
  501. return nil
  502. }
  503. // 缓存文件的位置信息,都是在程序的根目录下的 cache 中
  504. const (
  505. cacheRootFolderName = "cache" // 缓存文件夹总名称
  506. TmpFolder = "tmp" // 临时缓存的文件夹
  507. RodCacheFolder = "rod" // rod 的缓存目录
  508. PluginFolder = "Plugin" // 插件的目录
  509. DebugFolder = "CSF-DebugThings" // 调试相关的文件夹
  510. SubFixCacheFolder = "CSF-SubFixCache" // 字幕时间校正的缓存文件夹,一般可以不清理
  511. ShareSubFileCache = "CSF-ShareSubCache" // 字幕共享的缓存目录,不建议删除
  512. CacheCenterFolder = "CSF-CacheCenter" // 下载缓存、队列缓存、下载次数缓存的文件夹
  513. ManualSubUploadCacheFolder = "CSF-ManualSubUploadCache" // 手动上传字幕的缓存文件夹
  514. VideoAndSubPreviewCacheFolder = "CSF-VideoAndSubPreviewCache" // 视频和字幕的预览缓存
  515. )
  516. const (
  517. Plugin_Adblock = "adblock"
  518. )
  519. // 配置文件的位置信息,这个会根据系统版本做区分
  520. const (
  521. configDirRootFPathWindows = "." // Windows 就是在当前的程序目录
  522. configDirRootFPathLinux = "/config" // Linux 是在 /config 下
  523. configDirRootFPathDarwin = "/.config/chinesesubfinder" // Darwin 是在 os.UserHomeDir()/.config/chinesesubfinder/ 下
  524. )