/** * Get the extension of an ElggFile * * @param ElggFile $file the file to check * * @return string */ function file_tools_get_file_extension($file) { $result = ''; if ($file->getSubtype() == 'file') { if ($filename = $file->getFilename()) { $exploded_filename = explode('.', $filename); $result = end($exploded_filename); } } return strtolower($result); }
/** * Tests if an \ElggFile file exists. * * @param \ElggFile $file File object * * @return bool */ public function exists(\ElggFile $file) { if (!$file->getFilename()) { return false; } return file_exists($this->getFilenameOnFilestore($file)); }
/** * Convert a video/audio file to a web compatible format * * @param ElggFile $file File entity * @param string $format Format to convert to (extension) * @return ElggFile|false */ function elgg_file_viewer_convert_file($file, $format) { if (!$file instanceof ElggFile || !$format) { return false; } $ffmpeg_path = elgg_get_plugin_setting('ffmpeg_path', 'elgg_file_viewer'); if (!$ffmpeg_path) { return false; } $info = pathinfo($file->getFilenameOnFilestore()); $filename = $info['filename']; $output = new ElggFile(); $output->owner_guid = $file->owner_guid; $output->setFilename("projekktor/{$file->guid}/{$filename}.{$format}"); $output->open('write'); $output->close(); try { $FFmpeg = new FFmpeg($ffmpeg_path); if (!$file->icontime) { $icon = new ElggFile(); $icon->owner_guid = $file->owner_guid; $icon->setFilename("projekktor/{$file->guid}/{$filename}.jpg"); $FFmpeg->input($file->getFilenameOnFilestore())->thumb(0, 1)->output($icon->getFilenameOnFilestore())->ready(); if ($icon->exists()) { $file->icontime = time(); $file->ffmpeg_thumb = $icon->getFilename(); $prefix = 'file/'; $filestorename = $file->icontime . $filename . '.jpg'; $thumbnail = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $file->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($icon->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $file->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } } } $FFmpeg->input($file->getFilenameOnFilestore())->output($output->getFilenameOnFilestore())->ready(); elgg_log("Converting file {$file->guid} to {$format}: {$FFmpeg->command}", 'NOTICE'); } catch (Exception $ex) { elgg_log($ex->getMessage(), 'ERROR'); return false; } return $output; }
/** * @group FileService */ public function testCanTransferFile() { $dataroot = elgg_get_config('dataroot'); $file = new \ElggFile(); $file->owner_guid = 3; $file->setFilename("file-to-transfer.txt"); $file->setFilename("file-to-transfer.txt"); // Fail with non-existent file $this->assertFalse($file->transfer(4)); $file->open('write'); $file->write('Transfer'); $file->close(); $this->assertTrue($file->transfer(4)); $this->assertEquals(4, $file->owner_guid); $this->assertEquals("file-to-transfer.txt", $file->getFilename()); $this->assertEquals("{$dataroot}1/4/file-to-transfer.txt", $file->getFilenameOnFilestore()); $this->assertTrue($file->exists()); $this->assertFalse(file_exists("{$dataroot}1/3/file-to-transfer.txt")); $this->assertTrue($file->transfer(3, 'tmp/transferred-file.txt')); $this->assertEquals(3, $file->owner_guid); $this->assertEquals("tmp/transferred-file.txt", $file->getFilename()); $this->assertEquals("{$dataroot}1/3/tmp/transferred-file.txt", $file->getFilenameOnFilestore()); $this->assertTrue($file->exists()); $this->assertFalse(file_exists("{$dataroot}1/4/file-to-transfer.txt")); // cleanup _elgg_rmdir("{$dataroot}1/3/"); _elgg_rmdir("{$dataroot}1/4/"); }
function hj_framework_handle_multifile_upload($user_guid) { if (!empty($_FILES)) { $access = elgg_get_ignore_access(); elgg_set_ignore_access(true); $file = $_FILES['Filedata']; $filehandler = new hjFile(); $filehandler->owner_guid = (int) $user_guid; $filehandler->container_guid = (int) $user_guid; $filehandler->access_id = ACCESS_DEFAULT; $filehandler->data_pattern = hj_framework_get_data_pattern('object', 'hjfile'); $filehandler->title = $file['name']; $filehandler->description = ''; $prefix = "hjfile/"; $filestorename = elgg_strtolower($file['name']); $mime = hj_framework_get_mime_type($file['name']); $filehandler->setFilename($prefix . $filestorename); $filehandler->setMimeType($mime); $filehandler->originalfilename = $file['name']; $filehandler->simpletype = hj_framework_get_simple_type($mime); $filehandler->filesize = round($file['size'] / (1024 * 1024), 2) . "Mb"; $filehandler->open("write"); $filehandler->close(); move_uploaded_file($file['tmp_name'], $filehandler->getFilenameOnFilestore()); $file_guid = $filehandler->save(); hj_framework_set_entity_priority($filehandler); elgg_trigger_plugin_hook('hj:framework:file:process', 'object', array('entity' => $filehandler)); if ($file_guid) { $meta_value = $filehandler->getGUID(); } else { $meta_value = $filehandler->getFilenameOnFilestore(); } if ($file_guid && $filehandler->simpletype == "image") { $thumb_sizes = hj_framework_get_thumb_sizes(); foreach ($thumb_sizes as $thumb_type => $thumb_size) { $thumbnail = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $thumb_size['w'], $thumb_size['h'], $thumb_size['square'], 0, 0, 0, 0, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($file['type']); $thumb->owner_guid = $user_guid; $thumb->setFilename("{$prefix}{$filehandler->getGUID()}{$thumb_type}.jpg"); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $thumb_meta = "{$thumb_type}thumb"; $filehandler->{$thumb_meta} = $thumb->getFilename(); unset($thumbnail); } } } $response = array('status' => 'OK', 'value' => $meta_value); } else { $response = array('status' => 'FAIL'); } echo json_encode($response); elgg_set_ignore_access($access); return; }
/** * Returns the filename as saved on disk for an ElggFile object * * @param ElggFile $file File object * * @return string The full path of where the file is stored */ public function getFilenameOnFilestore(ElggFile $file) { $owner = $file->getOwnerEntity(); if (!$owner) { $owner = elgg_get_logged_in_user_entity(); } if (!$owner) { $msg = elgg_echo('InvalidParameterException:MissingOwner', array($file->getFilename(), $file->guid)); throw new InvalidParameterException($msg); } return $this->dir_root . $this->makefileMatrix($owner->guid) . $file->getFilename(); }
public function getFilenameOnFilestore(ElggFile $file) { $owner = $file->getOwnerEntity(); if (!$owner) { $owner = get_loggedin_user(); } if (!$owner || !$owner->username) { throw new InvalidParameterException(elgg_echo('InvalidParameterException:MissingOwner')); } return $this->dir_root . $this->make_file_matrix($owner->username) . $file->getFilename(); }