2011-01-23 03:33:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group aphront
|
|
|
|
*/
|
2012-03-13 19:18:11 +01:00
|
|
|
final class AphrontFileResponse extends AphrontResponse {
|
2011-01-23 03:33:00 +01:00
|
|
|
|
|
|
|
private $content;
|
|
|
|
private $mimeType;
|
|
|
|
private $download;
|
Fix two issues with audio macros
Summary:
Fixes T3887. Two issues:
- Macros were generating entirely before the render cache, so audio macros worked fine in previews and the first time the cache was populated, but not afterward.
- Instead, parse them before the cache but drop them in after the cache. Clean up all the file querying, too. This makes cached remarkup generate the correct audio beahviors.
- Safari sends an HTTP request with a "Range" header, and expects a "206 Partial Content" response. If we don't give it one, it sometimes has trouble figuring out how long a piece of audio is (mostly for longer clips? Or mostly for MP3s?). I'm not exactly sure what triggers it. The net effect is that "loop" does not work when Safari gets confused. While looping a short "quack.wav" worked fine, longer MP3s didn't loop.
- Supporting "Range" and "206 Partial Content", which is straightforward, fixes this problem.
Test Plan:
- Viewed a page with lots of different cached audio macros and lots of different uncached preview audio macros, they all rendered correctly and played audio.
- Viewed a macro with a long MP3 audio loop in Safari. Verified it looped after it completed. Used Charles to check that the server received and responded to the "Range" header correctly.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T3887
Differential Revision: https://secure.phabricator.com/D7166
2013-09-29 00:32:48 +02:00
|
|
|
private $rangeMin;
|
|
|
|
private $rangeMax;
|
2011-01-23 03:33:00 +01:00
|
|
|
|
|
|
|
public function setDownload($download) {
|
2011-02-22 18:19:14 +01:00
|
|
|
$download = preg_replace('/[^A-Za-z0-9_.-]/', '_', $download);
|
|
|
|
if (!strlen($download)) {
|
|
|
|
$download = 'untitled_document.txt';
|
|
|
|
}
|
2011-01-23 03:33:00 +01:00
|
|
|
$this->download = $download;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDownload() {
|
|
|
|
return $this->download;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMimeType($mime_type) {
|
|
|
|
$this->mimeType = $mime_type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMimeType() {
|
|
|
|
return $this->mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContent($content) {
|
|
|
|
$this->content = $content;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildResponseString() {
|
Fix two issues with audio macros
Summary:
Fixes T3887. Two issues:
- Macros were generating entirely before the render cache, so audio macros worked fine in previews and the first time the cache was populated, but not afterward.
- Instead, parse them before the cache but drop them in after the cache. Clean up all the file querying, too. This makes cached remarkup generate the correct audio beahviors.
- Safari sends an HTTP request with a "Range" header, and expects a "206 Partial Content" response. If we don't give it one, it sometimes has trouble figuring out how long a piece of audio is (mostly for longer clips? Or mostly for MP3s?). I'm not exactly sure what triggers it. The net effect is that "loop" does not work when Safari gets confused. While looping a short "quack.wav" worked fine, longer MP3s didn't loop.
- Supporting "Range" and "206 Partial Content", which is straightforward, fixes this problem.
Test Plan:
- Viewed a page with lots of different cached audio macros and lots of different uncached preview audio macros, they all rendered correctly and played audio.
- Viewed a macro with a long MP3 audio loop in Safari. Verified it looped after it completed. Used Charles to check that the server received and responded to the "Range" header correctly.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T3887
Differential Revision: https://secure.phabricator.com/D7166
2013-09-29 00:32:48 +02:00
|
|
|
if ($this->rangeMin || $this->rangeMax) {
|
|
|
|
$length = ($this->rangeMax - $this->rangeMin) + 1;
|
|
|
|
return substr($this->content, $this->rangeMin, $length);
|
|
|
|
} else {
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRange($min, $max) {
|
|
|
|
$this->rangeMin = $min;
|
|
|
|
$this->rangeMax = $max;
|
|
|
|
return $this;
|
2011-01-23 03:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getHeaders() {
|
|
|
|
$headers = array(
|
|
|
|
array('Content-Type', $this->getMimeType()),
|
Fix two issues with audio macros
Summary:
Fixes T3887. Two issues:
- Macros were generating entirely before the render cache, so audio macros worked fine in previews and the first time the cache was populated, but not afterward.
- Instead, parse them before the cache but drop them in after the cache. Clean up all the file querying, too. This makes cached remarkup generate the correct audio beahviors.
- Safari sends an HTTP request with a "Range" header, and expects a "206 Partial Content" response. If we don't give it one, it sometimes has trouble figuring out how long a piece of audio is (mostly for longer clips? Or mostly for MP3s?). I'm not exactly sure what triggers it. The net effect is that "loop" does not work when Safari gets confused. While looping a short "quack.wav" worked fine, longer MP3s didn't loop.
- Supporting "Range" and "206 Partial Content", which is straightforward, fixes this problem.
Test Plan:
- Viewed a page with lots of different cached audio macros and lots of different uncached preview audio macros, they all rendered correctly and played audio.
- Viewed a macro with a long MP3 audio loop in Safari. Verified it looped after it completed. Used Charles to check that the server received and responded to the "Range" header correctly.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T3887
Differential Revision: https://secure.phabricator.com/D7166
2013-09-29 00:32:48 +02:00
|
|
|
array('Content-Length', strlen($this->buildResponseString())),
|
2011-01-23 03:33:00 +01:00
|
|
|
);
|
|
|
|
|
Fix two issues with audio macros
Summary:
Fixes T3887. Two issues:
- Macros were generating entirely before the render cache, so audio macros worked fine in previews and the first time the cache was populated, but not afterward.
- Instead, parse them before the cache but drop them in after the cache. Clean up all the file querying, too. This makes cached remarkup generate the correct audio beahviors.
- Safari sends an HTTP request with a "Range" header, and expects a "206 Partial Content" response. If we don't give it one, it sometimes has trouble figuring out how long a piece of audio is (mostly for longer clips? Or mostly for MP3s?). I'm not exactly sure what triggers it. The net effect is that "loop" does not work when Safari gets confused. While looping a short "quack.wav" worked fine, longer MP3s didn't loop.
- Supporting "Range" and "206 Partial Content", which is straightforward, fixes this problem.
Test Plan:
- Viewed a page with lots of different cached audio macros and lots of different uncached preview audio macros, they all rendered correctly and played audio.
- Viewed a macro with a long MP3 audio loop in Safari. Verified it looped after it completed. Used Charles to check that the server received and responded to the "Range" header correctly.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T3887
Differential Revision: https://secure.phabricator.com/D7166
2013-09-29 00:32:48 +02:00
|
|
|
if ($this->rangeMin || $this->rangeMax) {
|
|
|
|
$len = strlen($this->content);
|
|
|
|
$min = $this->rangeMin;
|
|
|
|
$max = $this->rangeMax;
|
|
|
|
$headers[] = array('Content-Range', "bytes {$min}-{$max}/{$len}");
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:19:14 +01:00
|
|
|
if (strlen($this->getDownload())) {
|
|
|
|
$headers[] = array('X-Download-Options', 'noopen');
|
2011-02-22 18:22:57 +01:00
|
|
|
|
2011-02-22 18:19:14 +01:00
|
|
|
$filename = $this->getDownload();
|
2011-01-23 03:33:00 +01:00
|
|
|
$headers[] = array(
|
|
|
|
'Content-Disposition',
|
2011-02-22 18:19:14 +01:00
|
|
|
'attachment; filename='.$filename,
|
2011-01-23 03:33:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-09-14 01:38:28 +02:00
|
|
|
$headers = array_merge(parent::getHeaders(), $headers);
|
2011-01-23 03:33:00 +01:00
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|