Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete thumbnails of clips along with clips themselves #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MrJamesT
Copy link
Owner

@MrJamesT MrJamesT commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced cleanup process for clips, ensuring both original clips and their thumbnails are deleted when a clip is cut or deleted.
    • Improved management of associated clips by removing them from the database when related games are deleted.
    • Dynamic version display in the header, reflecting the updated app version.
  • Bug Fixes

    • Fixed issues with lingering thumbnail files by adding checks to delete them when clips are removed from the database.

@MrJamesT MrJamesT self-assigned this Oct 18, 2024
Copy link

coderabbitai bot commented Oct 18, 2024

Walkthrough

The changes enhance the clip cleanup process in the clipOperations.ts and fsOperations.ts files. The cutClip and deleteClip functions now ensure that associated thumbnail files are deleted alongside the original clips. Additionally, the readGamesFolderAndSave function has been updated to remove clips from the database when their corresponding games are deleted, and a new loop in readClipsFolderAndSave deletes thumbnails for clips removed from the database. The version number in package.json has been updated, and the Header.vue component now dynamically displays the application version.

Changes

File Path Change Summary
src/main/clipOperations.ts Enhanced cutClip and deleteClip functions to delete corresponding thumbnail files when clips are removed.
src/main/fsOperations.ts Updated readGamesFolderAndSave to delete associated clips from the database when games are removed; added a loop in readClipsFolderAndSave to delete thumbnails for removed clips.
package.json Updated version from "1.0.2" to "1.0.3".
src/components/Header.vue Changed Badge component's value prop to a dynamic computed property appVersion for version display.

Poem

In the land of clips and thumbs,
A cleanup dance, oh how it hums!
When games are gone, their clips take flight,
Thumbnails vanish, out of sight!
A tidy space, a joyful cheer,
For every clip, we hold so dear! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
src/main/fsOperations.ts (2)

33-42: LGTM! Consider minor optimization for clip deletion.

The changes effectively implement the cleanup process for games and associated clips. The order of operations (deleting clips before games) maintains referential integrity.

Consider using a single database operation to delete both clips and games:

if (gamesToDelete.length > 0) {
  await prisma.$transaction([
    prisma.clip.deleteMany({ where: { gameName: { in: gamesToDelete } } }),
    prisma.game.deleteMany({ where: { name: { in: gamesToDelete } } })
  ]);
}

This approach could potentially improve performance by reducing the number of database calls.


71-77: LGTM! Consider adding error handling for thumbnail deletion.

The new code effectively implements the cleanup process for thumbnail files associated with deleted clips, aligning with the PR objective.

Consider adding error handling for the thumbnail deletion process:

for (const delClip of clipsToDelete) {
  const thumbPath = path.join(settings.gameFolder, 'thumbs', delClip + '.jpg')
  if (fs.existsSync(thumbPath)) {
    try {
      fs.unlinkSync(thumbPath)
    } catch (error) {
      log.error(`Failed to delete thumbnail for ${delClip}: ${error}`)
    }
  }
}

This will ensure that the process continues even if a single thumbnail deletion fails, and it will log any errors for debugging purposes.

src/main/clipOperations.ts (3)

60-64: Approve changes with a minor suggestion for consistency.

The added code successfully implements the deletion of thumbnail files when the original clip is removed, which aligns with the PR objective. Good job on including the existence check before attempting to delete the thumbnail.

For consistency with the clip deletion, consider moving the thumbnail deletion logic into a separate function. This would make the code more modular and easier to maintain. For example:

function deleteThumbnail(settings: AppSettings, clip: Clip) {
  const thumbPath = path.join(settings.gameFolder, 'thumbs', clip.filename + '.jpg')
  if (fs.existsSync(thumbPath)) {
    fs.unlinkSync(thumbPath)
  }
}

// Then in cutClip:
if (reqData.removeOriginal) {
  fs.unlinkSync(oldClipPath)
  deleteThumbnail(settings, clip)
  await prisma.clip.delete({ where: { id: clipId } })
}

This approach would also make it easier to reuse the thumbnail deletion logic in other parts of the codebase if needed.


123-127: Approve changes with a suggestion for code reuse.

The added code successfully implements the deletion of thumbnail files when deleting a clip, which aligns with the PR objective. Good job on including the existence check before attempting to delete the thumbnail.

To improve code reuse and maintainability, consider extracting the thumbnail deletion logic into a separate function that can be used by both cutClip and deleteClip. This would reduce duplication and make future updates easier. For example:

function deleteThumbnail(settings: AppSettings, clip: Clip) {
  const thumbPath = path.join(settings.gameFolder, 'thumbs', clip.filename + '.jpg')
  if (fs.existsSync(thumbPath)) {
    fs.unlinkSync(thumbPath)
  }
}

// Then in deleteClip:
fs.unlinkSync(path.join(settings.gameFolder, clip.gameName, clip.filename))
deleteThumbnail(settings, clip)
await prisma.clip.delete({ where: { id: clipId } })

This refactoring would make the code more DRY (Don't Repeat Yourself) and easier to maintain.


Line range hint 1-190: Overall improvement suggestion for thumbnail handling.

The changes successfully implement the deletion of thumbnail files along with their corresponding clips in both the cutClip and deleteClip functions. This addresses the PR objective effectively.

To further improve the code, consider the following suggestions:

  1. Extract the thumbnail deletion logic into a separate function to promote code reuse and maintainability.
  2. Create a utility function for constructing thumbnail paths to ensure consistency across the codebase.
  3. Consider adding a configuration option for the thumbnail file extension (currently hardcoded as '.jpg') to make the code more flexible.

Here's an example of how these improvements could be implemented:

// At the top of the file
const THUMBNAIL_EXTENSION = '.jpg' // Could be moved to a config file

function getThumbnailPath(settings: AppSettings, clip: Clip): string {
  return path.join(settings.gameFolder, 'thumbs', clip.filename + THUMBNAIL_EXTENSION)
}

function deleteThumbnail(settings: AppSettings, clip: Clip): void {
  const thumbPath = getThumbnailPath(settings, clip)
  if (fs.existsSync(thumbPath)) {
    fs.unlinkSync(thumbPath)
  }
}

// Then use these functions in cutClip and deleteClip

These changes would make the code more modular, easier to maintain, and more consistent in its handling of thumbnails.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f253664 and 6085d6f.

📒 Files selected for processing (2)
  • src/main/clipOperations.ts (2 hunks)
  • src/main/fsOperations.ts (2 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/main/fsOperations.ts (1)

Line range hint 1-150: Summary: Implementation effectively addresses PR objective

The changes in this file successfully implement the deletion of thumbnails along with clips, as per the PR objective. The code maintains data integrity and includes appropriate checks before deletions. Minor optimization suggestions were provided for potential performance improvements and error handling.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
src/renderer/src/components/Header.vue (1)

60-61: LGTM: Computed property for app version implemented correctly.

The appVersion computed property correctly retrieves the version from the electron environment and formats it consistently. This implementation allows for easy updates when the version changes.

Consider adding a comment explaining the source of npm_package_version for better code maintainability:

 const appVersion = computed(() => `v${window.electron.process.env.npm_package_version} | BETA`)
+// npm_package_version is automatically set by Electron from package.json
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6085d6f and c5984db.

📒 Files selected for processing (2)
  • package.json (1 hunks)
  • src/renderer/src/components/Header.vue (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • package.json
🧰 Additional context used
🔇 Additional comments (1)
src/renderer/src/components/Header.vue (1)

5-5: LGTM: Dynamic version display implemented correctly.

The Badge component now uses the appVersion computed property, allowing for dynamic updating of the version displayed in the UI. This is a good practice and aligns with the PR objective of improving version display functionality.

Comment on lines +60 to +61
const appVersion = computed(() => `v${window.electron.process.env.npm_package_version} | BETA`)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling for robustness.

While the implementation is correct, it might be beneficial to add error handling in case npm_package_version is undefined. This would make the code more robust and prevent potential runtime errors.

Consider updating the computed property as follows:

-const appVersion = computed(() => `v${window.electron.process.env.npm_package_version} | BETA`)
+const appVersion = computed(() => {
+  const version = window.electron.process.env.npm_package_version || 'unknown'
+  return `v${version} | BETA`
+})

This change ensures that even if npm_package_version is undefined, the UI will still display a valid string.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const appVersion = computed(() => `v${window.electron.process.env.npm_package_version} | BETA`)
const appVersion = computed(() => {
const version = window.electron.process.env.npm_package_version || 'unknown'
return `v${version} | BETA`
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant