auto update image cache time

This commit is contained in:
niuhuan 2021-12-11 19:58:14 +08:00
parent 38981986b5
commit ceb5ba2051
3 changed files with 69 additions and 54 deletions

View File

@ -555,6 +555,19 @@ func DownloadInfo(comicId string) (*ComicDownload, error) {
return &download, nil return &download, nil
} }
func UpdateTimeCacheImageTime(id uint) {
now := time.Now()
mutex.Lock()
defer mutex.Unlock()
err := db.Model(&RemoteImage{}).Where("id = ?", id).Updates(map[string]interface{}{
"created_at": now,
"updated_at": now,
}).Error
if err != nil {
panic(err)
}
}
func VACUUM() error { func VACUUM() error {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()

View File

@ -11,8 +11,8 @@ import (
"net" "net"
"os" "os"
path2 "path" path2 "path"
comic_center2 "pikapika/pikapika/database/comic_center" "pikapika/pikapika/database/comic_center"
utils2 "pikapika/pikapika/utils" "pikapika/pikapika/utils"
"strconv" "strconv"
"strings" "strings"
) )
@ -96,17 +96,17 @@ func importComicDownload(zipPath string) error {
if err != nil { if err != nil {
return err return err
} }
return comic_center2.Transaction(func(tx *gorm.DB) error { return comic_center.Transaction(func(tx *gorm.DB) error {
// 删除 // 删除
err := tx.Unscoped().Delete(&comic_center2.ComicDownload{}, "id = ?", jsonComicDownload.ID).Error err := tx.Unscoped().Delete(&comic_center.ComicDownload{}, "id = ?", jsonComicDownload.ID).Error
if err != nil { if err != nil {
return err return err
} }
err = tx.Unscoped().Delete(&comic_center2.ComicDownloadEp{}, "comic_id = ?", jsonComicDownload.ID).Error err = tx.Unscoped().Delete(&comic_center.ComicDownloadEp{}, "comic_id = ?", jsonComicDownload.ID).Error
if err != nil { if err != nil {
return err return err
} }
err = tx.Unscoped().Delete(&comic_center2.ComicDownloadPicture{}, "comic_id = ?", jsonComicDownload.ID).Error err = tx.Unscoped().Delete(&comic_center.ComicDownloadPicture{}, "comic_id = ?", jsonComicDownload.ID).Error
if err != nil { if err != nil {
return err return err
} }
@ -129,7 +129,7 @@ func importComicDownload(zipPath string) error {
} }
} }
// VIEW日志 // VIEW日志
view := comic_center2.ComicView{} view := comic_center.ComicView{}
view.ID = jsonComicDownload.ID view.ID = jsonComicDownload.ID
view.CreatedAt = jsonComicDownload.CreatedAt view.CreatedAt = jsonComicDownload.CreatedAt
view.UpdatedAt = jsonComicDownload.UpdatedAt view.UpdatedAt = jsonComicDownload.UpdatedAt
@ -153,13 +153,13 @@ func importComicDownload(zipPath string) error {
view.IsFavourite = false view.IsFavourite = false
view.IsLiked = false view.IsLiked = false
view.CommentsCount = 0 view.CommentsCount = 0
err = comic_center2.NoLockActionViewComicUpdateInfoDB(&view, tx) err = comic_center.NoLockActionViewComicUpdateInfoDB(&view, tx)
if err != nil { if err != nil {
return err return err
} }
// 覆盖文件 // 覆盖文件
comicDirPath := downloadPath(jsonComicDownload.ID) comicDirPath := downloadPath(jsonComicDownload.ID)
utils2.Mkdir(comicDirPath) utils.Mkdir(comicDirPath)
logoReader, err := zip.Open("logo") logoReader, err := zip.Open("logo")
if err == nil { if err == nil {
defer logoReader.Close() defer logoReader.Close()
@ -167,10 +167,10 @@ func importComicDownload(zipPath string) error {
if err != nil { if err != nil {
return err return err
} }
ioutil.WriteFile(path2.Join(comicDirPath, "logo"), logoBuff, utils2.CreateFileMode) ioutil.WriteFile(path2.Join(comicDirPath, "logo"), logoBuff, utils.CreateFileMode)
} }
for _, ep := range jsonComicDownload.EpList { for _, ep := range jsonComicDownload.EpList {
utils2.Mkdir(path2.Join(comicDirPath, strconv.Itoa(int(ep.EpOrder)))) utils.Mkdir(path2.Join(comicDirPath, strconv.Itoa(int(ep.EpOrder))))
for _, picture := range ep.PictureList { for _, picture := range ep.PictureList {
notifyExport("写入 : " + picture.LocalPath) notifyExport("写入 : " + picture.LocalPath)
zipEntry, err := zip.Open(picture.SrcPath) zipEntry, err := zip.Open(picture.SrcPath)
@ -183,7 +183,7 @@ func importComicDownload(zipPath string) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(downloadPath(picture.LocalPath), entryBuff, utils2.CreateFileMode) return ioutil.WriteFile(downloadPath(picture.LocalPath), entryBuff, utils.CreateFileMode)
}() }()
if err != nil { if err != nil {
return err return err

View File

@ -10,10 +10,10 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
path2 "path" path2 "path"
comic_center2 "pikapika/pikapika/database/comic_center" "pikapika/pikapika/database/comic_center"
"pikapika/pikapika/database/network_cache" "pikapika/pikapika/database/network_cache"
"pikapika/pikapika/database/properties" "pikapika/pikapika/database/properties"
utils2 "pikapika/pikapika/utils" "pikapika/pikapika/utils"
"strconv" "strconv"
"time" "time"
) )
@ -34,7 +34,7 @@ func InitPlugin(_remoteDir string, _downloadDir string, _tmpDir string) {
remoteDir = _remoteDir remoteDir = _remoteDir
downloadDir = _downloadDir downloadDir = _downloadDir
tmpDir = _tmpDir tmpDir = _tmpDir
comic_center2.ResetAll() comic_center.ResetAll()
downloadAndExportPath = loadDownloadAndExportPath() downloadAndExportPath = loadDownloadAndExportPath()
downloadThreadCount = loadDownloadThreadCount() downloadThreadCount = loadDownloadThreadCount()
go downloadBackground() go downloadBackground()
@ -166,7 +166,7 @@ func preLogin() (string, error) {
token, _ := properties.LoadToken() token, _ := properties.LoadToken()
tokenTime, _ := properties.LoadTokenTime() tokenTime, _ := properties.LoadTokenTime()
if token != "" && tokenTime > 0 { if token != "" && tokenTime > 0 {
if utils2.Timestamp()-(1000*60*60*24) < tokenTime { if utils.Timestamp()-(1000*60*60*24) < tokenTime {
client.Token = token client.Token = token
return "true", nil return "true", nil
} }
@ -189,7 +189,7 @@ func login() error {
return err return err
} }
properties.SaveToken(client.Token) properties.SaveToken(client.Token)
properties.SaveTokenTime(utils2.Timestamp()) properties.SaveTokenTime(utils.Timestamp())
return nil return nil
} }
@ -216,16 +216,18 @@ func remoteImageData(params string) (string, error) {
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
fileServer := paramsStruct.FileServer fileServer := paramsStruct.FileServer
path := paramsStruct.Path path := paramsStruct.Path
lock := utils2.HashLock(fmt.Sprintf("%s$%s", fileServer, path)) lock := utils.HashLock(fmt.Sprintf("%s$%s", fileServer, path))
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
cache := comic_center2.FindRemoteImage(fileServer, path) cache := comic_center.FindRemoteImage(fileServer, path)
if cache == nil { if cache == nil {
remote, err := decodeAndSaveImage(fileServer, path) remote, err := decodeAndSaveImage(fileServer, path)
if err != nil { if err != nil {
return "", err return "", err
} }
cache = remote cache = remote
} else {
go comic_center.UpdateTimeCacheImageTime(cache.ID)
} }
display := DisplayImageData{ display := DisplayImageData{
FileSize: cache.FileSize, FileSize: cache.FileSize,
@ -245,10 +247,10 @@ func remoteImagePreload(params string) error {
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
fileServer := paramsStruct.FileServer fileServer := paramsStruct.FileServer
path := paramsStruct.Path path := paramsStruct.Path
lock := utils2.HashLock(fmt.Sprintf("%s$%s", fileServer, path)) lock := utils.HashLock(fmt.Sprintf("%s$%s", fileServer, path))
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
cache := comic_center2.FindRemoteImage(fileServer, path) cache := comic_center.FindRemoteImage(fileServer, path)
var err error var err error
if cache == nil { if cache == nil {
_, err = decodeAndSaveImage(fileServer, path) _, err = decodeAndSaveImage(fileServer, path)
@ -256,7 +258,7 @@ func remoteImagePreload(params string) error {
return err return err
} }
func decodeAndSaveImage(fileServer string, path string) (*comic_center2.RemoteImage, error) { func decodeAndSaveImage(fileServer string, path string) (*comic_center.RemoteImage, error) {
buff, img, format, err := decodeFromUrl(fileServer, path) buff, img, format, err := decodeFromUrl(fileServer, path)
if err != nil { if err != nil {
println(fmt.Sprintf("decode error : %s/static/%s %s", fileServer, path, err.Error())) println(fmt.Sprintf("decode error : %s/static/%s %s", fileServer, path, err.Error()))
@ -274,7 +276,7 @@ func decodeAndSaveImage(fileServer string, path string) (*comic_center2.RemoteIm
if err != nil { if err != nil {
return nil, err return nil, err
} }
remote := comic_center2.RemoteImage{ remote := comic_center.RemoteImage{
FileServer: fileServer, FileServer: fileServer,
Path: path, Path: path,
FileSize: int64(len(buff)), FileSize: int64(len(buff)),
@ -283,7 +285,7 @@ func decodeAndSaveImage(fileServer string, path string) (*comic_center2.RemoteIm
Height: int32(img.Bounds().Dy()), Height: int32(img.Bounds().Dy()),
LocalPath: local, LocalPath: local,
} }
err = comic_center2.SaveRemoteImage(&remote) err = comic_center.SaveRemoteImage(&remote)
return &remote, err return &remote, err
} }
@ -293,8 +295,8 @@ func downloadImagePath(path string) (string, error) {
func createDownload(params string) error { func createDownload(params string) error {
var paramsStruct struct { var paramsStruct struct {
Comic comic_center2.ComicDownload `json:"comic"` Comic comic_center.ComicDownload `json:"comic"`
EpList []comic_center2.ComicDownloadEp `json:"epList"` EpList []comic_center.ComicDownloadEp `json:"epList"`
} }
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
comic := paramsStruct.Comic comic := paramsStruct.Comic
@ -302,19 +304,19 @@ func createDownload(params string) error {
if comic.Title == "" || len(epList) == 0 { if comic.Title == "" || len(epList) == 0 {
return errors.New("params error") return errors.New("params error")
} }
err := comic_center2.CreateDownload(&comic, &epList) err := comic_center.CreateDownload(&comic, &epList)
if err != nil { if err != nil {
return err return err
} }
// 创建文件夹 // 创建文件夹
utils2.Mkdir(downloadPath(comic.ID)) utils.Mkdir(downloadPath(comic.ID))
// 复制图标 // 复制图标
downloadComicLogo(&comic) downloadComicLogo(&comic)
return nil return nil
} }
func downloadComicLogo(comic *comic_center2.ComicDownload) { func downloadComicLogo(comic *comic_center.ComicDownload) {
lock := utils2.HashLock(fmt.Sprintf("%s$%s", comic.ThumbFileServer, comic.ThumbPath)) lock := utils.HashLock(fmt.Sprintf("%s$%s", comic.ThumbFileServer, comic.ThumbPath))
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
buff, image, format, err := decodeFromCache(comic.ThumbFileServer, comic.ThumbPath) buff, image, format, err := decodeFromCache(comic.ThumbFileServer, comic.ThumbPath)
@ -323,8 +325,8 @@ func downloadComicLogo(comic *comic_center2.ComicDownload) {
} }
if err == nil { if err == nil {
comicLogoPath := path2.Join(comic.ID, "logo") comicLogoPath := path2.Join(comic.ID, "logo")
ioutil.WriteFile(downloadPath(comicLogoPath), buff, utils2.CreateFileMode) ioutil.WriteFile(downloadPath(comicLogoPath), buff, utils.CreateFileMode)
comic_center2.UpdateDownloadLogo( comic_center.UpdateDownloadLogo(
comic.ID, comic.ID,
int64(len(buff)), int64(len(buff)),
format, format,
@ -345,8 +347,8 @@ func downloadComicLogo(comic *comic_center2.ComicDownload) {
func addDownload(params string) error { func addDownload(params string) error {
var paramsStruct struct { var paramsStruct struct {
Comic comic_center2.ComicDownload `json:"comic"` Comic comic_center.ComicDownload `json:"comic"`
EpList []comic_center2.ComicDownloadEp `json:"epList"` EpList []comic_center.ComicDownloadEp `json:"epList"`
} }
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
comic := paramsStruct.Comic comic := paramsStruct.Comic
@ -354,11 +356,11 @@ func addDownload(params string) error {
if comic.Title == "" || len(epList) == 0 { if comic.Title == "" || len(epList) == 0 {
return errors.New("params error") return errors.New("params error")
} }
return comic_center2.AddDownload(&comic, &epList) return comic_center.AddDownload(&comic, &epList)
} }
func deleteDownloadComic(comicId string) error { func deleteDownloadComic(comicId string) error {
err := comic_center2.Deleting(comicId) err := comic_center.Deleting(comicId)
if err != nil { if err != nil {
return err return err
} }
@ -367,23 +369,23 @@ func deleteDownloadComic(comicId string) error {
} }
func loadDownloadComic(comicId string) (string, error) { func loadDownloadComic(comicId string) (string, error) {
download, err := comic_center2.FindComicDownloadById(comicId) download, err := comic_center.FindComicDownloadById(comicId)
if err != nil { if err != nil {
return "", err return "", err
} }
if download == nil { if download == nil {
return "", nil return "", nil
} }
comic_center2.ViewComic(comicId) // VIEW comic_center.ViewComic(comicId) // VIEW
return serialize(download, err) return serialize(download, err)
} }
func allDownloads() (string, error) { func allDownloads() (string, error) {
return serialize(comic_center2.AllDownloads()) return serialize(comic_center.AllDownloads())
} }
func downloadEpList(comicId string) (string, error) { func downloadEpList(comicId string) (string, error) {
return serialize(comic_center2.ListDownloadEpByComicId(comicId)) return serialize(comic_center.ListDownloadEpByComicId(comicId))
} }
func viewLogPage(params string) (string, error) { func viewLogPage(params string) (string, error) {
@ -392,11 +394,11 @@ func viewLogPage(params string) (string, error) {
Limit int `json:"limit"` Limit int `json:"limit"`
} }
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
return serialize(comic_center2.ViewLogPage(paramsStruct.Offset, paramsStruct.Limit)) return serialize(comic_center.ViewLogPage(paramsStruct.Offset, paramsStruct.Limit))
} }
func downloadPicturesByEpId(epId string) (string, error) { func downloadPicturesByEpId(epId string) (string, error) {
return serialize(comic_center2.ListDownloadPictureByEpId(epId)) return serialize(comic_center.ListDownloadPictureByEpId(epId))
} }
func getDownloadRunning() bool { func getDownloadRunning() bool {
@ -418,13 +420,13 @@ func cleanNetworkCache() error {
func cleanImageCache() error { func cleanImageCache() error {
notifyExport("清理图片缓存") notifyExport("清理图片缓存")
err := comic_center2.RemoveAllRemoteImage() err := comic_center.RemoveAllRemoteImage()
if err != nil { if err != nil {
return err return err
} }
notifyExport("清理图片文件") notifyExport("清理图片文件")
os.RemoveAll(remoteDir) os.RemoveAll(remoteDir)
utils2.Mkdir(remoteDir) utils.Mkdir(remoteDir)
notifyExport("清理结束") notifyExport("清理结束")
return nil return nil
} }
@ -437,13 +439,13 @@ func clean() error {
return err return err
} }
notifyExport("清理图片缓存") notifyExport("清理图片缓存")
err = comic_center2.RemoveAllRemoteImage() err = comic_center.RemoveAllRemoteImage()
if err != nil { if err != nil {
return err return err
} }
notifyExport("清理图片文件") notifyExport("清理图片文件")
os.RemoveAll(remoteDir) os.RemoveAll(remoteDir)
utils2.Mkdir(remoteDir) utils.Mkdir(remoteDir)
notifyExport("清理结束") notifyExport("清理结束")
return nil return nil
} }
@ -457,15 +459,15 @@ func autoClean(expire int64) error {
} }
pageSize := 10 pageSize := 10
for true { for true {
images, err := comic_center2.EarliestRemoteImage(earliest, pageSize) images, err := comic_center.EarliestRemoteImage(earliest, pageSize)
if err != nil { if err != nil {
return err return err
} }
if len(images) == 0 { if len(images) == 0 {
return comic_center2.VACUUM() return comic_center.VACUUM()
} }
// delete data & remove pic // delete data & remove pic
err = comic_center2.DeleteRemoteImages(images) err = comic_center.DeleteRemoteImages(images)
if err != nil { if err != nil {
return err return err
} }
@ -487,7 +489,7 @@ func storeViewEp(params string) error {
PictureRank int `json:"pictureRank"` PictureRank int `json:"pictureRank"`
} }
json.Unmarshal([]byte(params), &paramsStruct) json.Unmarshal([]byte(params), &paramsStruct)
return comic_center2.ViewEpAndPicture( return comic_center.ViewEpAndPicture(
paramsStruct.ComicId, paramsStruct.ComicId,
paramsStruct.EpOrder, paramsStruct.EpOrder,
paramsStruct.EpTitle, paramsStruct.EpTitle,
@ -496,7 +498,7 @@ func storeViewEp(params string) error {
} }
func loadView(comicId string) (string, error) { func loadView(comicId string) (string, error) {
view, err := comic_center2.LoadViewLog(comicId) view, err := comic_center.LoadViewLog(comicId)
if err != nil { if err != nil {
return "", nil return "", nil
} }
@ -634,10 +636,10 @@ func FlatInvoke(method string, params string) (string, error) {
case "viewLogPage": case "viewLogPage":
return viewLogPage(params) return viewLogPage(params)
case "clearAllViewLog": case "clearAllViewLog":
comic_center2.ClearAllViewLog() comic_center.ClearAllViewLog()
return "", nil return "", nil
case "deleteViewLog": case "deleteViewLog":
comic_center2.DeleteViewLog(params) comic_center.DeleteViewLog(params)
return "", nil return "", nil
case "cleanNetworkCache": case "cleanNetworkCache":
return "", cleanNetworkCache() return "", cleanNetworkCache()
@ -679,7 +681,7 @@ func FlatInvoke(method string, params string) (string, error) {
case "downloadPicturesByEpId": case "downloadPicturesByEpId":
return downloadPicturesByEpId(params) return downloadPicturesByEpId(params)
case "resetAllDownloads": case "resetAllDownloads":
return "", comic_center2.ResetAll() return "", comic_center.ResetAll()
case "exportComicDownload": case "exportComicDownload":
return exportComicDownload(params) return exportComicDownload(params)
case "exportComicDownloadToJPG": case "exportComicDownloadToJPG":