Skip to content

Commit 9fa222d

Browse files
committed
Fix primary download link for mods
Links were kinda supported, but no more
1 parent 349a7c5 commit 9fa222d

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎backend/app/Http/Controllers/FileController.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ function downloadLatestFileVersion(Request $request, Mod $mod) {
430430
* Returns the primary file of the mod or the only file
431431
*/
432432
public function getPrimaryFile(Request $request, Mod $mod) {
433-
$file = $mod->download;
434-
if (!$file instanceof File) {
433+
$file = $mod->download_strictly_file;
434+
if (!isset($file)) {
435435
abort(404);
436436
}
437437
return $file;

‎backend/app/Http/Controllers/ModController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public function report(Request $request, Mod $mod)
686686
* @group Files
687687
*/
688688
public function downloadPrimaryFile(Mod $mod) {
689-
$file = $mod->download;
689+
$file = $mod->download_strictly_file;
690690

691691
if (isset($file)) {
692692
return redirect($file->downloadUrl);

‎backend/app/Models/Mod.php‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ public function modManagers(): Attribute {
674674
/**
675675
* Smartly returns current download ($this->download)
676676
* In case it's not loaded, tries to calculate it using download_id and download_type
677-
* If download_id is not set, it will return either first link or first file.
677+
*
678+
* It may not be set, the default behavior of the site is to show "downloads" when there are multiple files and no primary file set
678679
*/
679680
public function download(): Attribute {
680681
return Attribute::make(function() {
@@ -694,9 +695,8 @@ public function download(): Attribute {
694695
$linksCount = $this->links_count;
695696
$hasPrimary = isset($id) && isset($type);
696697

697-
698698
// Has no files or links
699-
if ($filesCount == 0 && $linksCount == 0) {
699+
if ($linksLoaded && $filesLoaded && $filesCount == 0 && $linksCount == 0) {
700700
return null;
701701
}
702702

@@ -730,6 +730,47 @@ public function download(): Attribute {
730730
});
731731
}
732732

733+
/**
734+
* Similar to download but returns only files. Meant to be used for API use where downloading links aren't supported.
735+
* If download_id is not set, it will return first file.
736+
*/
737+
public function downloadStrictlyFile(): Attribute {
738+
return Attribute::make(function() {
739+
$filesLoaded = $this->relationLoaded('files');
740+
$id = $this->download_id;
741+
$type = $this->download_type;
742+
$hasPrimaryFileSet = isset($id) && isset($type) && $type != 'link';
743+
744+
// If download exists, just return it
745+
if ($hasPrimaryFileSet && ($this->relationLoaded('downloadRelation') || !$filesLoaded)) {
746+
if (isset($this->downloadRelation)) {
747+
return $this->downloadRelation;
748+
}
749+
}
750+
751+
// Has no files or links
752+
if ($filesLoaded && $this->files_count == 0) {
753+
return null;
754+
}
755+
756+
// Has primary download and both links and files relations are loaded
757+
if ($hasPrimaryFileSet) {
758+
if ($filesLoaded && ($link = $this->files->find($id))) {
759+
return $link;
760+
} else {
761+
return $this->withSecureConstraints(fn() => $this->files()->find($id));
762+
}
763+
}
764+
765+
if ($filesLoaded) {
766+
return $this->files[0];
767+
} else {
768+
return $this->withSecureConstraints(fn() => $this->files()->first());
769+
}
770+
});
771+
}
772+
773+
733774
public function liked()
734775
{
735776
return $this->hasOne(ModLike::class)->where('user_id', Auth::id());

0 commit comments

Comments
 (0)