Skip to main content

Overview

The MediaDownloadRef model tracks download references for both VOD streams and series episodes. It supports aria2 download management, retry logic, and error tracking. Model Path: app/Models/MediaDownloadRef.php
Table: media_download_refs

Database Schema

id
integer
required
Auto-incrementing primary key
gid
string
required
Unique aria2 download GID (globally unique identifier)
user_id
integer
Foreign key to users table (nullable)
media_id
integer
required
Polymorphic foreign key to the media (Series or VodStream)
media_type
string
required
Polymorphic type (e.g., ‘App\Models\Series’ or ‘App\Models\VodStream’)
downloadable_id
integer
required
ID of the downloadable item (stream_id or episode_id)
season
integer
Season number (only for series episodes, nullable for VODs)
episode
integer
Episode number (only for series episodes, nullable for VODs)
desired_paused
boolean
default:"false"
Whether the download should be paused
canceled_at
timestamp
Timestamp when the download was canceled
cancel_delete_partial
boolean
default:"false"
Whether to delete partial files on cancellation
last_error_code
integer
Last error code encountered during download
last_error_message
text
Last error message encountered
retry_attempt
integer
default:"0"
Number of retry attempts made
retry_next_at
timestamp
Scheduled timestamp for next retry attempt (indexed)
download_files
array
JSON array of downloaded file information
created_at
timestamp
Record creation timestamp
updated_at
timestamp
Record last update timestamp

Fillable Attributes

[
    'gid',
    'user_id',
    'media_id',
    'media_type',
    'downloadable_id',
    'season',
    'episode',
    'desired_paused',
    'canceled_at',
    'cancel_delete_partial',
    'last_error_code',
    'last_error_message',
    'retry_attempt',
    'retry_next_at',
    'download_files',
]

Type Casts

  • desired_pausedboolean
  • canceled_atimmutable_datetime
  • cancel_delete_partialboolean
  • last_error_codeinteger
  • retry_attemptinteger
  • retry_next_atimmutable_datetime
  • download_filesarray

Factory Methods

Create from VOD Stream

Create a download reference for a VOD stream:
public static function fromVodStream(
    string $gid,
    VodStream $vodStream,
    User|int|null $owner = null
): self
Example:
$vod = VodStream::find(123);
$download = MediaDownloadRef::fromVodStream(
    gid: 'abc123def456',
    vodStream: $vod,
    owner: auth()->user()
);

Create from Series Episode

Create a download reference for a series episode:
public static function fromSeriesAndEpisode(
    string $gid,
    Series $series,
    Episode $episode,
    User|int|null $owner = null
): self
Example:
$series = Series::find(456);
$episode = new Episode(/* episode data */);
$download = MediaDownloadRef::fromSeriesAndEpisode(
    gid: 'xyz789abc012',
    series: $series,
    episode: $episode,
    owner: 1 // user ID
);

Helper Methods

Check Media Type

public function isVodStream(): bool
Returns true if the download is for a VOD stream.
public function isSeriesWithEpisode(): bool
Returns true if the download is for a series episode. Example:
if ($download->isVodStream()) {
    // Handle VOD download
} elseif ($download->isSeriesWithEpisode()) {
    // Handle series episode download
}

Relationships

Media (Polymorphic)

Get the associated media model (either VodStream or Series):
public function media(): MorphTo
Usage:
$download = MediaDownloadRef::find(1);
$media = $download->media; // Returns VodStream or Series instance

Owner

Get the user who owns this download:
public function owner(): BelongsTo
Usage:
$download = MediaDownloadRef::find(1);
$user = $download->owner;

Download Management

Retry Logic

Track failed downloads with automatic retry:
$download->update([
    'last_error_code' => 500,
    'last_error_message' => 'Connection timeout',
    'retry_attempt' => $download->retry_attempt + 1,
    'retry_next_at' => now()->addMinutes(5),
]);

Pause and Cancel

// Pause a download
$download->update(['desired_paused' => true]);

// Cancel a download
$download->update([
    'canceled_at' => now(),
    'cancel_delete_partial' => true,
]);

Query Pending Retries

// Get downloads ready for retry
$pendingRetries = MediaDownloadRef::whereNotNull('retry_next_at')
    ->where('retry_next_at', '<=', now())
    ->get();

Build docs developers (and LLMs) love