@@ -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