Example #1
0
 static function fromFilehandle($fh, Profile $scoped)
 {
     $stream = stream_get_meta_data($fh);
     // So far we're only handling filehandles originating from tmpfile(),
     // so we can always do hash_file on $stream['uri'] as far as I can tell!
     $filehash = hash_file(File::FILEHASH_ALG, $stream['uri']);
     try {
         $file = File::getByHash($filehash);
         // Already have it, so let's reuse the locally stored File
         $filename = $file->filename;
         $mimetype = $file->mimetype;
     } catch (NoResultException $e) {
         File::respectsQuota($scoped, filesize($stream['uri']));
         $mimetype = self::getUploadedMimeType($stream['uri']);
         switch (common_config('attachments', 'filename_base')) {
             case 'upload':
                 $filename = File::filename($scoped, "email", $mimetype);
                 break;
             case 'hash':
             default:
                 $filename = strtolower($filehash) . '.' . File::guessMimeExtension($mimetype);
         }
         $filepath = File::path($filename);
         $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
         if (!$result) {
             // TRANS: Client exception thrown when a file upload operation fails because the file could
             // TRANS: not be moved from the temporary folder to the permanent file location.
             throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath));
         }
     }
     return new MediaFile($scoped, $filename, $mimetype, $filehash);
 }