Skip to content

Commit 01862b8

Browse files
author
epriestley
committedJun 30, 2016
Detect the MIME type of large files by examining the first chunk
Summary: Fixes T11242. See that task for detailed discussion. Previously, it didn't particularly matter that we don't MIME detect chunked files since they were all just big blobs of junk (PSDs, zips/tarballs, whatever) that we handled uniformly. However, videos are large and the MIME type also matters. - Detect the overall mime type by detecitng the MIME type of the first chunk. This appears to work properly, at least for video. - Skip mime type detection on other chunks, which we were performing and ignoring. This makes uploading chunked files a little faster since we don't need to write stuff to disk. Test Plan: Uploaded a 50MB video locally, saw it as chunks with a "video/mp4" mime type, played it in the browser in Phabricator as an embedded HTML 5 video. {F1706837} Reviewers: chad Reviewed By: chad Maniphest Tasks: T11242 Differential Revision: https://secure.phabricator.com/D16204
1 parent 7a31578 commit 01862b8

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed
 

‎src/applications/files/conduit/FileUploadChunkConduitAPIMethod.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,43 @@ protected function execute(ConduitAPIRequest $request) {
5151
$start,
5252
$start + $length);
5353

54+
// If this is the initial chunk, leave the MIME type unset so we detect
55+
// it and can update the parent file. If this is any other chunk, it has
56+
// no meaningful MIME type. Provide a default type so we can avoid writing
57+
// it to disk to perform MIME type detection.
58+
if (!$start) {
59+
$mime_type = null;
60+
} else {
61+
$mime_type = 'application/octet-stream';
62+
}
63+
5464
// NOTE: These files have a view policy which prevents normal access. They
5565
// are only accessed through the storage engine.
5666
$chunk_data = PhabricatorFile::newFromFileData(
5767
$data,
5868
array(
5969
'name' => $file->getMonogram().'.chunk-'.$chunk->getID(),
6070
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
71+
'mime-type' => $mime_type,
6172
));
6273

6374
$chunk->setDataFilePHID($chunk_data->getPHID())->save();
6475

76+
$needs_update = false;
77+
6578
$missing = $this->loadAnyMissingChunk($viewer, $file);
6679
if (!$missing) {
67-
$file->setIsPartial(0)->save();
80+
$file->setIsPartial(0);
81+
$needs_update = true;
82+
}
83+
84+
if (!$start) {
85+
$file->setMimeType($chunk_data->getMimeType());
86+
$needs_update = true;
87+
}
88+
89+
if ($needs_update) {
90+
$file->save();
6891
}
6992

7093
return null;

‎src/applications/files/storage/PhabricatorFile.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,8 @@ public static function newChunkedFile(
270270

271271
$file->setByteSize($length);
272272

273-
// TODO: We might be able to test the first chunk in order to figure
274-
// this out more reliably, since MIME detection usually examines headers.
275-
// However, enormous files are probably always either actually raw data
276-
// or reasonable to treat like raw data.
273+
// NOTE: Once we receive the first chunk, we'll detect its MIME type and
274+
// update the parent file. This matters for large media files like video.
277275
$file->setMimeType('application/octet-stream');
278276

279277
$chunked_hash = idx($params, 'chunkedHash');

0 commit comments

Comments
 (0)
Failed to load comments.