folder.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. package my_folder
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/get_access_time"
  5. "github.com/sirupsen/logrus"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "time"
  11. )
  12. // --------------------------------------------------------------
  13. // Debug
  14. // --------------------------------------------------------------
  15. // GetRootDebugFolder 在程序的根目录新建,调试用文件夹
  16. func GetRootDebugFolder() (string, error) {
  17. nowProcessRoot, err := os.Getwd()
  18. if err != nil {
  19. return "", err
  20. }
  21. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, DebugFolder)
  22. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  23. if err != nil {
  24. return "", err
  25. }
  26. return nowProcessRoot, err
  27. }
  28. // GetDebugFolderByName 根据传入的 strings (["aa", "bb"]) 组成 DebugFolder/aa/bb 这样的路径
  29. func GetDebugFolderByName(names []string) (string, error) {
  30. rootPath, err := GetRootDebugFolder()
  31. if err != nil {
  32. return "", err
  33. }
  34. tmpFolderFullPath := rootPath
  35. for _, name := range names {
  36. tmpFolderFullPath = filepath.Join(tmpFolderFullPath, name)
  37. }
  38. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  39. if err != nil {
  40. return "", err
  41. }
  42. return tmpFolderFullPath, nil
  43. }
  44. // CopyFiles2DebugFolder 把文件放入到 Debug 文件夹中,新建 desFolderName 文件夹
  45. func CopyFiles2DebugFolder(names []string, subFiles []string) error {
  46. debugFolderByName, err := GetDebugFolderByName(names)
  47. if err != nil {
  48. return err
  49. }
  50. // 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
  51. for _, subFile := range subFiles {
  52. newFn := filepath.Join(debugFolderByName, filepath.Base(subFile))
  53. err = CopyFile(subFile, newFn)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. return nil
  59. }
  60. // --------------------------------------------------------------
  61. // Tmp
  62. // --------------------------------------------------------------
  63. // GetRootTmpFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
  64. func GetRootTmpFolder() (string, error) {
  65. nowProcessRoot, err := os.Getwd()
  66. if err != nil {
  67. return "", err
  68. }
  69. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, TmpFolder)
  70. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  71. if err != nil {
  72. return "", err
  73. }
  74. return nowProcessRoot, err
  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. // Adblock Cache
  127. // --------------------------------------------------------------
  128. // GetPluginRootFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
  129. func GetPluginRootFolder() (string, error) {
  130. nowProcessRoot, err := os.Getwd()
  131. if err != nil {
  132. return "", err
  133. }
  134. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, PluginFolder)
  135. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  136. if err != nil {
  137. return "", err
  138. }
  139. return nowProcessRoot, err
  140. }
  141. // GetPluginFolderByName 获取缓存的文件夹,没有则新建
  142. func GetPluginFolderByName(folderName string) (string, error) {
  143. rootPath, err := GetPluginRootFolder()
  144. if err != nil {
  145. return "", err
  146. }
  147. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  148. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  149. if err != nil {
  150. return "", err
  151. }
  152. return tmpFolderFullPath, nil
  153. }
  154. // ClearPluginFolderByName 清理指定的缓存文件夹
  155. func ClearPluginFolderByName(folderName string) error {
  156. nowTmpFolder, err := GetPluginFolderByName(folderName)
  157. if err != nil {
  158. return err
  159. }
  160. return ClearFolder(nowTmpFolder)
  161. }
  162. // --------------------------------------------------------------
  163. // Rod Cache
  164. // --------------------------------------------------------------
  165. // GetRodTmpRootFolder 在程序的根目录新建,rod 缓存用文件夹
  166. func GetRodTmpRootFolder() (string, error) {
  167. nowProcessRoot, err := os.Getwd()
  168. if err != nil {
  169. return "", err
  170. }
  171. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, RodCacheFolder)
  172. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  173. if err != nil {
  174. return "", err
  175. }
  176. return nowProcessRoot, err
  177. }
  178. // ClearRodTmpRootFolder 清理 rod 缓存文件夹
  179. func ClearRodTmpRootFolder() error {
  180. nowTmpFolder, err := GetRodTmpRootFolder()
  181. if err != nil {
  182. return err
  183. }
  184. pathSep := string(os.PathSeparator)
  185. files, err := os.ReadDir(nowTmpFolder)
  186. if err != nil {
  187. return err
  188. }
  189. for _, curFile := range files {
  190. fullPath := nowTmpFolder + pathSep + curFile.Name()
  191. if curFile.IsDir() {
  192. err = os.RemoveAll(fullPath)
  193. if err != nil {
  194. return err
  195. }
  196. } else {
  197. // 这里就是文件了
  198. err = os.Remove(fullPath)
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. }
  204. return nil
  205. }
  206. // --------------------------------------------------------------
  207. // Sub Fix Cache
  208. // --------------------------------------------------------------
  209. // GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
  210. func GetRootSubFixCacheFolder() (string, error) {
  211. nowProcessRoot, err := os.Getwd()
  212. if err != nil {
  213. return "", err
  214. }
  215. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, SubFixCacheFolder)
  216. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  217. if err != nil {
  218. return "", err
  219. }
  220. return nowProcessRoot, err
  221. }
  222. // GetRootCacheCenterFolder 下载缓存、队列缓存、下载次数缓存的文件夹
  223. func GetRootCacheCenterFolder() (string, error) {
  224. nowProcessRoot, err := os.Getwd()
  225. if err != nil {
  226. return "", err
  227. }
  228. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, CacheCenterFloder)
  229. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  230. if err != nil {
  231. return "", err
  232. }
  233. return nowProcessRoot, err
  234. }
  235. // GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
  236. func GetSubFixCacheFolderByName(folderName string) (string, error) {
  237. rootPath, err := GetRootSubFixCacheFolder()
  238. if err != nil {
  239. return "", err
  240. }
  241. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  242. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  243. if err != nil {
  244. return "", err
  245. }
  246. return tmpFolderFullPath, nil
  247. }
  248. // --------------------------------------------------------------
  249. // Share Sub Cache
  250. // --------------------------------------------------------------
  251. // GetShareSubRootFolder 在程序的根目录新建,字幕共享的缓存根目录,下级还有具体是按发行的时间去划分的子集目录
  252. func GetShareSubRootFolder() (string, error) {
  253. nowProcessRoot, err := os.Getwd()
  254. if err != nil {
  255. return "", err
  256. }
  257. nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, ShareSubFileCache)
  258. err = os.MkdirAll(nowProcessRoot, os.ModePerm)
  259. if err != nil {
  260. return "", err
  261. }
  262. return nowProcessRoot, err
  263. }
  264. // GetShareFolderByYear 缓存的文件夹以发行的年为一个单位存储
  265. func GetShareFolderByYear(year int) (string, error) {
  266. rootPath, err := GetShareSubRootFolder()
  267. if err != nil {
  268. return "", err
  269. }
  270. tmpFolderFullPath := filepath.Join(rootPath, fmt.Sprintf("%d", year))
  271. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  272. if err != nil {
  273. return "", err
  274. }
  275. return tmpFolderFullPath, nil
  276. }
  277. // ClearShareSubFolderByYear 清理指定的缓存文件夹
  278. func ClearShareSubFolderByYear(year int) error {
  279. nowTmpFolder, err := GetShareFolderByYear(year)
  280. if err != nil {
  281. return err
  282. }
  283. return ClearFolder(nowTmpFolder)
  284. }
  285. // ClearShareSubFolderByYearAndName 清理指定的缓存文件夹
  286. func ClearShareSubFolderByYearAndName(year int, name string) error {
  287. nowTmpFolder, err := GetShareFolderByYear(year)
  288. if err != nil {
  289. return err
  290. }
  291. return ClearFolder(filepath.Join(nowTmpFolder, name))
  292. }
  293. // --------------------------------------------------------------
  294. // Common
  295. // --------------------------------------------------------------
  296. // ClearFolder 清空文件夹
  297. func ClearFolder(folderFullPath string) error {
  298. pathSep := string(os.PathSeparator)
  299. files, err := os.ReadDir(folderFullPath)
  300. if err != nil {
  301. return err
  302. }
  303. for _, curFile := range files {
  304. fullPath := folderFullPath + pathSep + curFile.Name()
  305. if curFile.IsDir() {
  306. err = os.RemoveAll(fullPath)
  307. if err != nil {
  308. return err
  309. }
  310. } else {
  311. // 这里就是文件了
  312. err = os.Remove(fullPath)
  313. if err != nil {
  314. return err
  315. }
  316. }
  317. }
  318. return nil
  319. }
  320. // GetConfigRootDirFPath 获取 Config 的根目录,不同系统不一样
  321. func GetConfigRootDirFPath() string {
  322. nowConfigFPath := ""
  323. sysType := runtime.GOOS
  324. if sysType == "linux" {
  325. nowConfigFPath = configDirRootFPathLinux
  326. } else if sysType == "windows" {
  327. nowConfigFPath = configDirRootFPathWindows
  328. } else if sysType == "darwin" {
  329. home, err := os.UserHomeDir()
  330. if err != nil {
  331. panic("GetConfigRootDirFPath darwin get UserHomeDir, Error:" + err.Error())
  332. }
  333. nowConfigFPath = home + configDirRootFPathDarwin
  334. } else {
  335. panic("GetConfigRootDirFPath can't matched OSType: " + sysType + " ,You Should Implement It Yourself")
  336. }
  337. return nowConfigFPath
  338. }
  339. // ClearIdleSubFixCacheFolder 清理闲置的字幕修正缓存文件夹
  340. func ClearIdleSubFixCacheFolder(l *logrus.Logger, rootSubFixCacheFolder string, outOfDate time.Duration) error {
  341. /*
  342. 从 GetRootSubFixCacheFolder 目录下,遍历第一级目录中的文件夹
  343. 然后每个文件夹中,统计里面最后的访问时间(可能有多个文件),如果超过某个时间范围就标记删除这个文件夹
  344. */
  345. pathSep := string(os.PathSeparator)
  346. files, err := os.ReadDir(rootSubFixCacheFolder)
  347. if err != nil {
  348. return err
  349. }
  350. wait2ScanFolder := make([]string, 0)
  351. for _, curFile := range files {
  352. fullPath := rootSubFixCacheFolder + pathSep + curFile.Name()
  353. if curFile.IsDir() == true {
  354. // 需要关注文件夹
  355. wait2ScanFolder = append(wait2ScanFolder, fullPath)
  356. }
  357. }
  358. wait2DeleteFolder := make([]string, 0)
  359. getAccessTimeEx := get_access_time.GetAccessTimeEx{}
  360. cutOff := time.Now().Add(-outOfDate)
  361. for _, s := range wait2ScanFolder {
  362. files, err = os.ReadDir(s)
  363. if err != nil {
  364. return err
  365. }
  366. maxAccessTime := time.Now()
  367. // 需要统计这个文件夹下的所有文件的 AccessTIme,找出最新(最大的值)的那个时间,再比较
  368. for i, curFile := range files {
  369. fullPath := s + pathSep + curFile.Name()
  370. if curFile.IsDir() == true {
  371. continue
  372. }
  373. // 只需要关注文件
  374. accessTime, err := getAccessTimeEx.GetAccessTime(fullPath)
  375. if err != nil {
  376. return err
  377. }
  378. if i == 0 {
  379. maxAccessTime = accessTime
  380. }
  381. if Time2SecondNumber(accessTime) > Time2SecondNumber(maxAccessTime) {
  382. maxAccessTime = accessTime
  383. }
  384. }
  385. if maxAccessTime.Sub(cutOff) <= 0 {
  386. // 确认可以删除
  387. wait2DeleteFolder = append(wait2DeleteFolder, s)
  388. }
  389. }
  390. // 统一清理过期的文件夹
  391. for _, s := range wait2DeleteFolder {
  392. l.Infoln("Try 2 clear SubFixCache Folder:", s)
  393. err := os.RemoveAll(s)
  394. if err != nil {
  395. return err
  396. }
  397. }
  398. return nil
  399. }
  400. // CopyFile copies a single file from src to dst
  401. func CopyFile(src, dst string) error {
  402. var err error
  403. var srcFd *os.File
  404. var dstFd *os.File
  405. var srcInfo os.FileInfo
  406. if srcFd, err = os.Open(src); err != nil {
  407. return err
  408. }
  409. defer func() {
  410. _ = srcFd.Close()
  411. }()
  412. if dstFd, err = os.Create(dst); err != nil {
  413. return err
  414. }
  415. defer func() {
  416. _ = dstFd.Close()
  417. }()
  418. if _, err = io.Copy(dstFd, srcFd); err != nil {
  419. return err
  420. }
  421. if srcInfo, err = os.Stat(src); err != nil {
  422. return err
  423. }
  424. return os.Chmod(dst, srcInfo.Mode())
  425. }
  426. func Time2SecondNumber(inTime time.Time) float64 {
  427. outSecond := 0.0
  428. outSecond += float64(inTime.Hour() * 60 * 60)
  429. outSecond += float64(inTime.Minute() * 60)
  430. outSecond += float64(inTime.Second())
  431. outSecond += float64(inTime.Nanosecond()) / 1000 / 1000 / 1000
  432. return outSecond
  433. }
  434. // 缓存文件的位置信息,都是在程序的根目录下的 cache 中
  435. const (
  436. cacheRootFolderName = "cache" // 缓存文件夹总名称
  437. TmpFolder = "tmp" // 临时缓存的文件夹
  438. RodCacheFolder = "rod" // rod 的缓存目录
  439. PluginFolder = "Plugin" // 插件的目录
  440. DebugFolder = "CSF-DebugThings" // 调试相关的文件夹
  441. SubFixCacheFolder = "CSF-SubFixCache" // 字幕时间校正的缓存文件夹,一般可以不清理
  442. ShareSubFileCache = "CSF-ShareSubCache" // 字幕共享的缓存目录,不建议删除
  443. CacheCenterFloder = "CSF-CacheCenter" // 下载缓存、队列缓存、下载次数缓存的文件夹
  444. )
  445. const (
  446. Plugin_Adblock = "adblock"
  447. )
  448. // 配置文件的位置信息,这个会根据系统版本做区分
  449. const (
  450. configDirRootFPathWindows = "." // Windows 就是在当前的程序目录
  451. configDirRootFPathLinux = "/config" // Linux 是在 /config 下
  452. configDirRootFPathDarwin = "/.config/chinesesubfinder" // Darwin 是在 os.UserHomeDir()/.config/chinesesubfinder/ 下
  453. )