Skip to content

Commit

Permalink
Refactoring directories setup - defalut and user-defined
Browse files Browse the repository at this point in the history
  • Loading branch information
boggydigital committed Sep 26, 2023
1 parent 05f846c commit fa3562e
Show file tree
Hide file tree
Showing 27 changed files with 264 additions and 196 deletions.
11 changes: 7 additions & 4 deletions cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ func Backup() error {
ba := nod.Begin("backing up local data...")
defer ba.End()

abd := vangogh_local_data.AbsBackupsDir()
abp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.Backups)
if err != nil {
return err
}

if _, err := os.Stat(abd); os.IsNotExist(err) {
if err := os.MkdirAll(abd, 0755); err != nil {
if _, err := os.Stat(abp); os.IsNotExist(err) {
if err := os.MkdirAll(abp, 0755); err != nil {
return ba.EndWithError(err)
}
}

return Export(abd)
return Export(abp)
}
28 changes: 22 additions & 6 deletions cli/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ func (cd *cleanupDelegate) Process(_ string, slug string, list vangogh_local_dat

for _, unexpectedFile := range unexpectedFiles {
//restore absolute from local_filename to s/slug/local_filename
absDownloadFilename := vangogh_local_data.AbsDownloadDirFromRel(filepath.Join(pDir, unexpectedFile))
absDownloadFilename, err := vangogh_local_data.AbsDownloadDirFromRel(filepath.Join(pDir, unexpectedFile))
if err != nil {
return csa.EndWithError(err)
}
if stat, err := os.Stat(absDownloadFilename); err == nil {
cd.totalBytes += stat.Size()
} else if os.IsNotExist(err) {
Expand All @@ -169,20 +172,28 @@ func (cd *cleanupDelegate) Process(_ string, slug string, list vangogh_local_dat
prefix = "TEST"
}

relDownloadFilename, err := filepath.Rel(vangogh_local_data.AbsDownloadsDir(), absDownloadFilename)
adp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.Downloads)
if err != nil {
return csa.EndWithError(err)
}

relDownloadFilename, err := filepath.Rel(adp, absDownloadFilename)
if err != nil {
return csa.EndWithError(err)
}

dft := nod.Begin(" %s %s", prefix, relDownloadFilename)
if !cd.test {
if err := vangogh_local_data.MoveToRecycleBin(vangogh_local_data.AbsDownloadsDir(), absDownloadFilename); err != nil {
if err := vangogh_local_data.MoveToRecycleBin(adp, absDownloadFilename); err != nil {
return dft.EndWithError(err)
}
}
dft.End()

absChecksumFile := vangogh_local_data.AbsLocalChecksumPath(absDownloadFilename)
absChecksumFile, err := vangogh_local_data.AbsLocalChecksumPath(absDownloadFilename)
if err != nil {
return csa.EndWithError(err)
}
if stat, err := os.Stat(absChecksumFile); err == nil {
cd.totalBytes += stat.Size()
} else if os.IsNotExist(err) {
Expand All @@ -191,14 +202,19 @@ func (cd *cleanupDelegate) Process(_ string, slug string, list vangogh_local_dat
return csa.EndWithError(err)
}

relChecksumFile, err := filepath.Rel(vangogh_local_data.AbsChecksumsDir(), absChecksumFile)
acp, err := vangogh_local_data.GetAbsRelDir(vangogh_local_data.Checksums)
if err != nil {
return csa.EndWithError(err)
}

relChecksumFile, err := filepath.Rel(acp, absChecksumFile)
if err != nil {
return csa.EndWithError(err)
}

cft := nod.Begin(" %s %s", prefix, relChecksumFile)
if !cd.test {
if err := vangogh_local_data.MoveToRecycleBin(vangogh_local_data.AbsChecksumsDir(), absChecksumFile); err != nil {
if err := vangogh_local_data.MoveToRecycleBin(acp, absChecksumFile); err != nil {
return cft.EndWithError(err)
}
}
Expand Down
7 changes: 6 additions & 1 deletion cli/dehydrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func Dehydrate(
continue
}

if dhi, err := dehydrateImage(vangogh_local_data.AbsLocalImagePath(imageId), plt); err == nil {
alip, err := vangogh_local_data.AbsLocalImagePath(imageId)
if err != nil {
return di.EndWithError(err)
}

if dhi, err := dehydrateImage(alip, plt); err == nil {
dehydratedImages[id] = []string{dhi}
dehydratedImageModified[id] = []string{strconv.FormatInt(time.Now().Unix(), 10)}
} else {
Expand Down
17 changes: 12 additions & 5 deletions cli/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ import (
)

func ExportHandler(_ *url.URL) error {
return Export(vangogh_local_data.AbsOutputFilesDir())
aofp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.OutputFiles)
if err != nil {
return err
}
return Export(aofp)
}

func Export(to string) error {

from := vangogh_local_data.AbsMetadataDir()
root, _ := filepath.Split(from)

ea := nod.NewProgress("exporting metadata...")
defer ea.End()

if err := packer.Pack(root, from, to, ea); err != nil {
amp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.Metadata)
if err != nil {
return ea.EndWithError(err)
}
root, _ := filepath.Split(amp)

if err := packer.Pack(root, amp, to, ea); err != nil {
return ea.EndWithError(err)
}

Expand Down
9 changes: 6 additions & 3 deletions cli/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ func GetData(
return nil
}

hc, err := coost.NewHttpClientFromFile(
vangogh_local_data.AbsCookiePath(),
gog_integration.GogHost)
acp, err := vangogh_local_data.AbsCookiePath()
if err != nil {
return gda.EndWithError(err)
}

hc, err := coost.NewHttpClientFromFile(acp, gog_integration.GogHost)
if err != nil {
return gda.EndWithError(err)
}
Expand Down
44 changes: 37 additions & 7 deletions cli/get_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func GetDownloads(
gda := nod.NewProgress("downloading product files...")
defer gda.End()

hc, err := coost.NewHttpClientFromFile(vangogh_local_data.AbsCookiePath(), gog_integration.GogHost)
acp, err := vangogh_local_data.AbsCookiePath()
if err != nil {
return gda.EndWithError(err)
}

hc, err := coost.NewHttpClientFromFile(acp, gog_integration.GogHost)
if err != nil {
return gda.EndWithError(err)
}
Expand Down Expand Up @@ -117,7 +122,12 @@ func (gdd *getDownloadsDelegate) Process(_, slug string, list vangogh_local_data
return nil
}

hc, err := coost.NewHttpClientFromFile(vangogh_local_data.AbsCookiePath(), gog_integration.GogHost)
acp, err := vangogh_local_data.AbsCookiePath()
if err != nil {
return sda.EndWithError(err)
}

hc, err := coost.NewHttpClientFromFile(acp, gog_integration.GogHost)
if err != nil {
return sda.EndWithError(err)
}
Expand Down Expand Up @@ -163,11 +173,17 @@ func (gdd *getDownloadsDelegate) downloadManualUrl(
if localPath, ok := gdd.rxa.GetFirstVal(vangogh_local_data.LocalManualUrlProperty, dl.ManualUrl); ok {
//localFilename would be a relative path for a download - s/slug,
//and RelToAbs would convert this to downloads/s/slug
if _, err := os.Stat(vangogh_local_data.AbsDownloadDirFromRel(localPath)); err == nil {
addp, err := vangogh_local_data.AbsDownloadDirFromRel(localPath)
if err != nil {
return dmua.EndWithError(err)
}
if _, err := os.Stat(addp); err == nil {
_, localFilename := filepath.Split(localPath)
lfa := nod.Begin(" - %s", localFilename)
lfa.EndWithResult("already exists")
return nil
} else {
return dmua.EndWithError(err)
}
}
}
Expand Down Expand Up @@ -200,12 +216,26 @@ func (gdd *getDownloadsDelegate) downloadManualUrl(
return dmua.EndWithError(err)
}
//we need to add suffix to a dir path, e.g. dlc, extras
absDir := filepath.Join(pAbsDir, dl.DirSuffix())
dtRelDir := ""
switch dl.Type {
case vangogh_local_data.DLC:
dtRelDir, err = vangogh_local_data.GetRelDir(vangogh_local_data.DLCs)
case vangogh_local_data.Extra:
dtRelDir, err = vangogh_local_data.GetRelDir(vangogh_local_data.Extras)
}
if err != nil {
return dmua.EndWithError(err)
}
absDir := filepath.Join(pAbsDir, dtRelDir)

//4
remoteChecksumPath := vangogh_local_data.RemoteChecksumPath(resolvedUrl.Path)
if remoteChecksumPath != "" {
localChecksumPath := vangogh_local_data.AbsLocalChecksumPath(path.Join(absDir, filename))
localChecksumPath, err := vangogh_local_data.AbsLocalChecksumPath(path.Join(absDir, filename))
if err != nil {
return dmua.EndWithError(err)
}

if _, err := os.Stat(localChecksumPath); os.IsNotExist(err) {
checksumDir, checksumFilename := filepath.Split(localChecksumPath)
dca := nod.NewProgress(" - %s", checksumFilename)
Expand Down Expand Up @@ -234,8 +264,8 @@ func (gdd *getDownloadsDelegate) downloadManualUrl(
//6
//ProductDownloadsRelDir would return relative (to downloads/ root) dir path, e.g. s/slug
pRelDir, err := vangogh_local_data.RelProductDownloadsDir(slug)
//we need to add suffix to a dir path, e.g. dlc, extras
relDir := filepath.Join(pRelDir, dl.DirSuffix())
//we need to add suffix to a dir path, e.g. dlc, extras - using already resolved download type relative dir
relDir := filepath.Join(pRelDir, dtRelDir)
if err != nil {
return dmua.EndWithError(err)
}
Expand Down
5 changes: 4 additions & 1 deletion cli/get_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func GetImages(
urls = append(urls, srcUrls...)

for _, srcUrl := range srcUrls {
dstDir := vangogh_local_data.AbsImagesDirByImageId(srcUrl.Path)
dstDir, err := vangogh_local_data.AbsImagesDirByImageId(srcUrl.Path)
if err != nil {
return mita.EndWithError(err)
}
filenames = append(filenames, filepath.Join(dstDir, srcUrl.Path))
}
}
Expand Down
6 changes: 5 additions & 1 deletion cli/get_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func GetItems(
for _, itemUrl := range items {
if u, err := url.Parse(itemUrl); err == nil {
urls = append(urls, u)
filenames = append(filenames, vangogh_local_data.AbsItemPath(u.Path))
aip, err := vangogh_local_data.AbsItemPath(u.Path)
if err != nil {
return gia.EndWithError(err)
}
filenames = append(filenames, aip)
}
}

Expand Down
5 changes: 4 additions & 1 deletion cli/get_thumbnails.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func GetThumbnails(idSet map[string]bool, missing bool, force bool) error {

vta := nod.NewProgress(" %s %s", videoId, file)

dir := vangogh_local_data.AbsVideoThumbnailsDirByVideoId(videoId)
dir, err := vangogh_local_data.AbsVideoThumbnailsDirByVideoId(videoId)
if err != nil {
return vta.EndWithError(err)
}

//get-thumbnails is not using dolo.GetSetMany unlike get-images, and is downloading
//thumbnails sequentially for two main reasons:
Expand Down
5 changes: 4 additions & 1 deletion cli/get_videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ func GetVideos(idSet map[string]bool, missing bool, force bool) error {
continue
}

dir := vangogh_local_data.AbsVideoDirByVideoId(videoId)
dir, err := vangogh_local_data.AbsVideoDirByVideoId(videoId)
if err != nil {
return vfa.EndWithError(err)
}

u, err := url.Parse(vidUrl.Url)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cli/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ func releasedToday(rxa kvas.ReduxAssets) ([]string, error) {

func publishAtom(gauginUrl string, rxa kvas.ReduxAssets, summary map[string][]string) error {

atomFile, err := os.Create(vangogh_local_data.AbsAtomFeedPath())
afp, err := vangogh_local_data.AbsAtomFeedPath()
if err != nil {
return err
}

atomFile, err := os.Create(afp)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,13 @@ func getDetailData(pts []vangogh_local_data.ProductType, since int64) error {

var skipList wits.KeyValues

if _, err := os.Stat(vangogh_local_data.AbsSkipListPath()); err == nil {
slFile, err := os.Open(vangogh_local_data.AbsSkipListPath())
aslp, err := vangogh_local_data.AbsSkipListPath()
if err != nil {
return err
}

if _, err := os.Stat(aslp); err == nil {
slFile, err := os.Open(aslp)
if err != nil {
slFile.Close()
return err
Expand Down
7 changes: 6 additions & 1 deletion cli/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func Tag(idSet map[string]bool, operation, tagName string) error {
}
}

hc, err := coost.NewHttpClientFromFile(vangogh_local_data.AbsCookiePath(), gog_integration.GogHost)
acp, err := vangogh_local_data.AbsCookiePath()
if err != nil {
return ta.EndWithError(err)
}

hc, err := coost.NewHttpClientFromFile(acp, gog_integration.GogHost)
if err != nil {
return ta.EndWithError(err)
}
Expand Down
10 changes: 8 additions & 2 deletions cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func validateManualUrl(

//absolute path (given a downloads/ root) for a s/slug/local_filename,
//e.g. downloads/s/slug/local_filename
absLocalFile := vangogh_local_data.AbsDownloadDirFromRel(localFile)
absLocalFile, err := vangogh_local_data.AbsDownloadDirFromRel(localFile)
if err != nil {
return mua.EndWithError(err)
}
if !vangogh_local_data.IsPathSupportingValidation(absLocalFile) {
mua.EndWithResult(ErrValidationNotSupported.Error())
return ErrValidationNotSupported
Expand All @@ -150,7 +153,10 @@ func validateManualUrl(
return ErrMissingDownload
}

absChecksumFile := vangogh_local_data.AbsLocalChecksumPath(absLocalFile)
absChecksumFile, err := vangogh_local_data.AbsLocalChecksumPath(absLocalFile)
if err != nil {
return mua.EndWithError(err)
}

if _, err := os.Stat(absChecksumFile); os.IsNotExist(err) {
mua.EndWithResult(ErrMissingChecksum.Error())
Expand Down
12 changes: 10 additions & 2 deletions cli/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ func Vet(
}

if vetOpts.oldBackups {
if err := vets.OldFiles(vangogh_local_data.AbsBackupsDir(), "backups", fix); err != nil {
abp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.Backups)
if err != nil {
return sda.EndWithError(err)
}

if err := vets.OldFiles(abp, "backups", fix); err != nil {
return sda.EndWithError(err)
}
}
Expand Down Expand Up @@ -200,7 +205,10 @@ func staleDehydrationsImageType(imageProperty, dimProperty string, fix bool) err

for _, id := range ids {
if imageId, ok := rxa.GetFirstVal(imageProperty, id); ok {
imagePath := vangogh_local_data.AbsLocalImagePath(imageId)
imagePath, err := vangogh_local_data.AbsLocalImagePath(imageId)
if err != nil {
return sdia.EndWithError(err)
}
if stat, err := os.Stat(imagePath); err == nil {
if dimStr, ok := rxa.GetFirstVal(dimProperty, id); ok {
if dim, err := strconv.ParseInt(dimStr, 10, 64); err == nil {
Expand Down
6 changes: 5 additions & 1 deletion cli/vets/files_in_recycle_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ func FilesInRecycleBin(fix bool) error {

if fix {
rfa := nod.NewProgress(" emptying recycle bin...")
rbdp, err := vangogh_local_data.GetAbsDir(vangogh_local_data.RecycleBin)
if err != nil {
return rfa.EndWithError(err)
}
rfa.TotalInt(len(recycleBinFiles))
for file := range recycleBinFiles {
if err := os.Remove(filepath.Join(vangogh_local_data.AbsRecycleBinDir(), file)); err != nil {
if err := os.Remove(filepath.Join(rbdp, file)); err != nil {
return rfa.EndWithError(err)
}
rfa.Increment()
Expand Down
Loading

0 comments on commit fa3562e

Please sign in to comment.