db = $db; $this->mix = $mix; $this->mix_id = $mix->getId(); $this->preDownload(); } private function preDownload(): void { $this->name = $this->mix->getName(); $buildDJs = $this->mix->getDJs(); $this->url = $this->mix->getUrl(); $this->djs = ''; $djCount = 0; foreach ($buildDJs as $dj) { if ($djCount > 0) { $this->djs .= ', '; } $this->djs .= $dj->getName(); $djCount++; } } public function download(): void { $this->loadDownload(); if (!$this->ready) { echo "I had a problem downloading the file."; return; } else { if ($this->checkForMixDownloadCount()) { $this->incrementMixDownloadCount(); } else { $this->addMixDownloadCount(); } header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); header("Content-Length: " . $this->filesize); header("Content-Disposition: attachment; filename=\"" . $this->filename . "\""); echo $this->content; } } private function loadDownload(): void { $this->content = file_get_contents($this->url); $this->filesize = strlen($this->content); $this->ext = pathinfo(basename($this->url), PATHINFO_EXTENSION); $this->filename = $this->djs . ' - ' . $this->name . ' (Downloaded from UtahsDJs.com).' . $this->ext; if ($this->filesize > 0) { $this->ready = true; } } private function checkForMixDownloadCount(): bool { $stmt = $this->db->prepare("SELECT * FROM mix_meta WHERE attribute = 'downloads' and mix_id = ?"); $stmt->bind_param('i', $this->mix_id); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $stmt->close(); if ($row) { return true; } else { return false; } } private function incrementMixDownloadCount(): void { $stmt = $this->db->prepare("UPDATE mix_meta SET value = value + 1 WHERE attribute = 'downloads' and mix_id = ?"); $stmt->bind_param('i', $this->mix_id); $stmt->execute(); $stmt->close(); } private function addMixDownloadCount(): void { $stmt = $this->db->prepare("INSERT INTO mix_meta (mix_id, attribute, value) VALUES (?, 'downloads', 1)"); $stmt->bind_param('i', $this->mix_id); $stmt->execute(); $stmt->close(); } public function getExt(): string { return $this->ext; } }