T125: Replaced downloads and streams with TrackFiles.

This commit is contained in:
Peter Deltchev 2015-05-25 22:35:52 -07:00
parent bf634d33f9
commit 9ade037820
4 changed files with 84 additions and 28 deletions

View file

@ -2,6 +2,7 @@
use Entities\ResourceLogItem;
use Entities\Track;
use Entities\TrackFile;
use Illuminate\Support\Facades\App;
class TracksController extends Controller {
@ -71,24 +72,22 @@
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$format = null;
$formatName = null;
$trackFile = null;
foreach (Track::$Formats as $name => $item) {
if ($item['extension'] == $extension) {
$format = $item;
$formatName = $name;
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
break;
}
}
if ($format == null)
if ($trackFile == null)
App::abort(404);
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $format['index']);
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $trackFile->getFormat()['index']);
$response = Response::make('', 200);
$filename = $track->getFileFor($formatName);
$filename = $trackFile->getFile();
if (Config::get('app.sendfile')) {
$response->header('X-Sendfile', $filename);
@ -104,7 +103,7 @@
}
$response->header('Last-Modified', $time);
$response->header('Content-Type', $format['mime_type']);
$response->header('Content-Type', $trackFile->getFormat()['mime_type']);
return $response;
}
@ -114,31 +113,29 @@
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$format = null;
$formatName = null;
$trackFile = null;
foreach (Track::$Formats as $name => $item) {
if ($item['extension'] == $extension) {
$format = $item;
$formatName = $name;
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
break;
}
}
if ($format == null)
if ($trackFile == null)
App::abort(404);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $format['index']);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
$response = Response::make('', 200);
$filename = $track->getFileFor($formatName);
$filename = $trackFile->getFile();
if (Config::get('app.sendfile')) {
$response->header('X-Sendfile', $filename);
$response->header('Content-Disposition', 'attachment; filename="' . $track->getDownloadFilenameFor($formatName) . '"');
$response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
} else {
$response->header('X-Accel-Redirect', $filename);
$response->header('Content-Disposition', 'attachment; filename="' . $track->getDownloadFilenameFor($formatName) . '"');
$response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
}
$time = gmdate(filemtime($filename));
@ -149,7 +146,7 @@
}
$response->header('Last-Modified', $time);
$response->header('Content-Type', $format['mime_type']);
$response->header('Content-Type', $trackFile->getFormat()['mime_type']);
return $response;
}

View file

@ -59,7 +59,7 @@
$trackFile->format = $name;
$track->trackFiles()->save($trackFile);
$target = $destination . '/' . $track->getFilenameFor($name);
$target = $destination . '/' . $trackFile->getFilename(); //$track->getFilenameFor($name);
$command = $format['command'];
$command = str_replace('{$source}', '"' . $source . '"', $command);

View file

@ -126,12 +126,12 @@
$formats = [];
foreach (self::$Formats as $name => $format) {
foreach ($track->trackFiles as $trackFile) {
$formats[] = [
'name' => $name,
'extension' => $format['extension'],
'url' => $track->getUrlFor($name),
'size' => Helpers::formatBytes($track->getFilesize($name))
'name' => $trackFile->format,
'extension' => $trackFile->extension,
'url' => $trackFile->url,
'size' => $trackFile->size
];
}

View file

@ -1,7 +1,66 @@
<?php namespace Entities;
use Helpers;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Cache;
class TrackFile extends \Eloquent {
public function track() {
return $this->belongsTo('Entities\Track');
}
public function getFormatAttribute($value) {
return $value;
}
public function getExtensionAttribute() {
return Track::$Formats[$this->format]['extension'];
}
public function getUrlAttribute() {
return URL::to('/t' . $this->track_id . '/dl.' . $this->extension);
}
public function getSizeAttribute($value) {
return Helpers::formatBytes($this->getFilesize($this->getFile()));
}
public function getFormat() {
return Track::$Formats[$this->format];
}
protected function getFilesize() {
return Cache::remember($this->getCacheKey('filesize'), 1440, function () {
$file = $this->getFile();
$size = 0;
if (is_file($file)) {
$size = filesize($file);
}
return $size;
});
}
public function getDirectory() {
$dir = (string) (floor($this->track_id / 100) * 100);
return \Config::get('app.files_directory') . '/tracks/' . $dir;
}
public function getFile() {
return "{$this->getDirectory()}/{$this->track_id}.{$this->extension}";
}
public function getFilename() {
return "{$this->track_id}.{$this->extension}";
}
public function getDownloadFilename() {
return "{$this->track->title}.{$this->extension}";
}
private function getCacheKey($key) {
return 'track_file-' . $this->id . '-' . $key;
}
}