VOOZH about

URL: https://deepwiki.com/Automattic/BuddyPress-VIP-Go/3.4-video-upload-and-cache-management

⇱ Video Upload and Cache Management | Automattic/BuddyPress-VIP-Go | DeepWiki


Loading...
Menu

Video Upload and Cache Management

Purpose and Scope

This document covers the video upload handling and cache management functionality in the BuddyPress VIP Go plugin. Unlike avatar and cover image management (see #3.1 and #3.2), video uploads require minimal intervention because they are stored directly by BuddyPress and do not require the metadata-based dynamic processing approach used for images. The plugin's video-specific functionality focuses on two key areas: ensuring temporary file cleanup uses VIP-compatible file operations, and invalidating caches after video operations complete.

For information about the broader file handling architecture, see Core Integration Layer.


System Overview

The video handling system in BuddyPress VIP Go consists of three specialized functions that intercept BuddyPress and BuddyBoss video operations:

FunctionHookPriorityPurpose
vipbp_override_remove_temp_directorybp_core_pre_remove_temp_directory10Replace filesystem directory operations with VIP-compatible single file deletion
vipbp_filter_chunked_upload_temp_dirbfu_temp_dir10Redirect BuddyBoss chunked uploads to local temp directory to avoid file locking errors on VIP FHS
vipbp_flush_cache_after_video_movebp_video_after_save99Reset media cache incrementor after video moves to ensure cache consistency

Unlike avatar and cover image operations which require extensive metadata storage and URL generation, video uploads flow through BuddyPress's standard upload mechanisms and are stored directly on VIP Go's file hosting service through WordPress's upload APIs. However, BuddyBoss's chunked upload implementation requires special handling to avoid file locking conflicts with VIP's filesystem abstraction.

Sources: files.php42-46


Temporary File Cleanup

VIP-Compatible Directory Removal

BuddyPress's default behavior for cleaning up temporary video files uses PHP's native filesystem functions to remove entire directories. In the VIP Go environment, these operations must be replaced with VIP-compatible alternatives.


Diagram: Temporary File Cleanup Override Flow

The vipbp_override_remove_temp_directory function intercepts the bp_core_pre_remove_temp_directory filter and performs the following operations:

  1. Construct File Path: Combines the directory path with the image name and .jpg extension files.php881
  2. Check File Existence: Verifies the file exists before attempting deletion files.php883
  3. Delete Using VIP API: Uses wp_delete_file() instead of PHP's native functions files.php885
  4. Return True: Signals BuddyPress to skip its default cleanup logic files.php886

If the file does not exist, the function returns false to allow BuddyPress's default behavior to proceed files.php890

Function Signature


Parameters:

  • $override (bool): Whether to override default behavior (passed by filter)
  • $directory (string): Path to the temporary directory
  • $image_name (string): Base name of the image file (without extension)

Returns: bool - true if override was applied, false to continue with default behavior

Sources: files.php872-891


Cache Management

Video Move Cache Invalidation

When videos are moved between albums or groups in BuddyPress, the media cache must be invalidated to prevent stale data from being served. The plugin hooks into the bp_video_after_save action to reset the BuddyPress media cache incrementor.


Diagram: Video Move Cache Invalidation Sequence

The cache invalidation mechanism relies on BuddyPress's incrementor pattern:

  • Cache Key Structure: BuddyPress uses cache keys like bp_media_{incrementor}_{query_params}
  • Incrementor Reset: Calling bp_core_reset_incrementor('bp_media') increments the bp_media value stored in the database
  • Cache Miss: Subsequent cache lookups use the new incrementor value, causing cache misses and forcing fresh queries
  • Priority 99: The hook runs late (priority 99) to ensure all other video processing completes first

This approach ensures that any cached media listings, video counts, or related queries are invalidated immediately after a video is moved, preventing users from seeing outdated information.

Sources: files.php45 files.php893-901


Chunked Upload Handling

BuddyBoss Large File Upload Support

BuddyBoss implements a chunked upload mechanism for large video files, splitting them into smaller pieces that are uploaded sequentially and then assembled on the server. This mechanism uses PHP's file locking (flock()) to coordinate writes during the assembly process. However, VIP Go's File Hosting Service (FHS) does not support file locking operations, causing "Failed to create lock file" errors.


Diagram: BuddyBoss Chunked Upload Flow with VIP Compatibility Fix

The solution redirects chunked uploads to the server's local temporary directory where file locking is supported:

  1. Filter Interception: Hook bfu_temp_dir filter before BuddyBoss determines upload location files.php43
  2. Local Directory Path: Return get_temp_dir() . 'bb-large-upload' instead of VIP FHS path files.php923
  3. File Assembly: BuddyBoss assembles chunks in local directory using flock() successfully
  4. Final Storage: After assembly completes, the complete file is moved to VIP FHS through standard WordPress upload APIs

Chunked Upload Function Implementation

The vipbp_filter_chunked_upload_temp_dir function provides a simple but critical override:


Key Implementation Details:

  • Uses WordPress's get_temp_dir() function to determine the system temp directory files.php923
  • Appends bb-large-upload subdirectory for organization and isolation
  • No parameters needed - the filter only needs to return the directory path
  • Applies globally to all BuddyBoss chunked upload operations

Why Local Temp Directory Works:

  • Local filesystem supports all PHP file operations including flock()
  • Temporary storage - files are moved to VIP FHS after assembly
  • Server's temp directory is designed for transient file operations
  • No long-term storage concerns as assembled files move to permanent location

BuddyBoss Compatibility Note: This functionality is specific to BuddyBoss's large file upload implementation. Standard BuddyPress video uploads do not use chunked uploads and are not affected by this filter.

Sources: files.php908-924 CHANGELOG.md18


Hook Integration Points

Filter: bp_core_pre_remove_temp_directory

This filter allows the plugin to completely override BuddyPress's temporary directory removal logic.

Hook Registration:


Filter Parameters:

  1. $override (bool): Return value from previous filters
  2. $directory (string): Directory path to remove
  3. $image_name (string): Image file base name

Expected Return: bool - true to prevent default behavior, false to allow it

Sources: files.php42

Filter: bfu_temp_dir

This BuddyBoss-specific filter allows customization of the temporary directory used for chunked file uploads.

Hook Registration:


Filter Parameters: None - filter callback returns a string path

Expected Return: string - Full path to temporary directory for chunked uploads

BuddyBoss Context: The bfu prefix refers to BuddyBoss File Upload functionality. This filter is called during the chunked upload process before BuddyBoss attempts to write file chunks.

Sources: files.php43 files.php908-924

Action: bp_video_after_save

This action fires after a video object has been saved to the database, including when videos are moved between albums or groups.

Hook Registration:


Priority 99: Ensures cache invalidation happens after all other video processing, including any additional metadata updates or related operations that may be hooked at lower priorities.

Sources: files.php46


Complete Video Operation Flow


Diagram: Complete Video Operation Flow with VIP Overrides

This diagram illustrates how the three VIP-specific video functions integrate into BuddyPress and BuddyBoss video handling workflows:

  1. Standard Upload Path: Small videos upload through standard WordPress APIs, which are already VIP-compatible
  2. BuddyBoss Chunked Upload Path: Large videos are redirected to local temp directory for assembly, then moved to VIP FHS
  3. Cleanup Phase: Temporary file removal is intercepted and redirected to wp_delete_file()
  4. Move Phase: Cache invalidation occurs automatically after video moves complete

Sources: files.php42-46 files.php872-924


Key Implementation Details

File Path Construction

The temporary file cleanup function constructs the full file path using trailingslashit() to ensure proper path formatting across different environments:


This approach:

  • Ensures consistent path separators regardless of OS
  • Explicitly adds the .jpg extension expected by BuddyPress
  • Creates a valid path for VIP Go's filesystem abstraction layer

Sources: files.php881

Minimal Intervention Philosophy

Unlike avatar and cover image handling which requires extensive metadata storage and dynamic URL generation (see #3.1 and #3.3), video handling requires minimal plugin intervention because:

  1. Direct Storage: Videos are stored as-is without resizing or cropping
  2. Standard APIs: WordPress's upload APIs handle VIP Go integration automatically for standard uploads
  3. No Metadata: Videos don't require crop coordinates or dimension tracking
  4. Simple URLs: Video URLs point directly to uploaded files without query parameters

The plugin only intervenes in three specific scenarios:

  • Filesystem Operations: Where BuddyPress uses PHP filesystem functions incompatible with VIP Go's abstraction layer (temporary file cleanup)
  • File Locking: Where BuddyBoss's chunked upload mechanism requires file locking unavailable on VIP FHS (chunked upload redirection)
  • Cache Management: Where video operations require cache invalidation to prevent stale data

Sources: files.php1-924


Multisite Considerations

Unlike avatar and cover image operations, the video handling functions do not implement blog switching logic. This is intentional:

  • Temporary Files: The vipbp_override_remove_temp_directory function operates on files in the current site's upload directory
  • Cache Operations: The bp_core_reset_incrementor() call operates on the current blog's options table
  • No Cross-Site Operations: Video moves occur within a single site context

If BuddyPress evolves to support cross-site video operations, additional blog switching logic would need to be added following the pattern established in avatar and cover image functions.

For information about the multisite architecture pattern used elsewhere, see Multisite Architecture.

Sources: files.php872-901