✏️ 正在编辑: Upload.php
路径:
/srv/systems_dir/yuppiecred-bkp/library/Upload.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Upload{ protected function getS3() { $config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini"); $s3Options = $config->toArray(); if (empty($s3Options['production']['amazon']['s3'])) { throw new Core_Exception("Amazon S3 não configurado nesse sistema!"); } return new Yuppie_Amazon_S3($s3Options['production']['amazon']['s3']); } public static function getConfigS3() { $config = new Zend_Config_Ini(CLIENT_PATH . "/configs/application.ini", "production"); if( empty($config->amazon->s3) ){ // throw new Core_Exception('S3 não configurado para seu sistema.'); return null; } return $config->toArray()['amazon']['s3']; } public static function reducePDF($fpath, $fname) { //Executa o scrip criando um arquivo temporário com um r na frente. shell_exec("gs \ -q -dNOPAUSE -dBATCH -dSAFER \ -sDEVICE=pdfwrite \ -dCompatibilityLevel=1.3 \ -dPDFSETTINGS=/ebook \ -dEmbedAllFonts=true \ -dSubsetFonts=true \ -dColorImageDownsampleType=/Bicubic \ -dColorImageResolution=150 \ -dGrayImageDownsampleType=/Bicubic \ -dGrayImageResolution=200 \ -dMonoImageDownsampleType=/Bicubic \ -dMonoImageResolution=150 \ -sOutputFile=\"{$fpath}/r{$fname}\" \ \"{$fpath}/$fname\""); unlink("$fpath/$fname"); return "r{$fname}"; } public static function reduceImage($fpath, $fname, $size) { $finput = "{$fpath}/{$fname}"; $foutput = "{$fpath}/r{$fname}"; $fileSize = filesize($finput); // Tamanho do arquivo original $fileInfo = getimagesize($finput); //Informações do arquivo $fileSize = $fileSize / 1024; //Tamanho do arquivo em KB if( $fileSize <= $size ){ return $fname; // Mantém o original } if( $fileInfo['mime'] == 'image/bmp' || $fileInfo['mime'] == 'image/png' ){ $fname = self::renameToJpg($finput); $finput = "{$fpath}/{$fname}"; $foutput = "{$fpath}/r{$fname}"; } shell_exec("convert \"{$finput}\" -strip -sampling-factor '4:2:0' -define 'jpeg:dct-method=float' -quality '92%' -define 'jpeg:extent=400kb' \"{$foutput}\""); //Exclui o arquivo original e retorna o reduzido. unlink($finput); return "r$fname"; } function renameToJpg($finput) { $info = pathinfo($finput); $fname = $info['filename'] . '.jpg'; rename($finput, $info['dirname'] . '/' . $fname); return $fname; } function sendToAmazon($file, $extension, $dir) { self::checkStorageLimit(); $directory = Yuppie_Auth::getSiteKey() . "/$dir"; $filename = time() . ".$extension"; $s3 = self::getS3(); return $s3->putObject("$directory/$filename", $file); } public static function save(Zend_File_Transfer_Adapter_Http $upload, &$fields, $dir) { if (!$upload->getFileName()) { return null; } self::checkStorageLimit(); foreach ($upload->getFileInfo() as $file => $info) { $info['destination'] = UPLOAD_PATH; $extension = pathinfo($info['name'], PATHINFO_EXTENSION); $fname = md5(microtime()) . Yuppie_Auth::getIdentity()->id . "." . $extension; $upload->addFilter(new Zend_Filter_File_Rename([ 'target' => UPLOAD_PATH . '/' . $fname, 'overwrite' => false ])); if ($upload->receive($file)) { if ($info['type'] == 'application/pdf') { $fname = self::reducePDF($info['destination'], $fname); } elseif (in_array($info['type'], array('image/jpeg', 'image/png', 'image/bmp'))) { $fname = self::reduceImage($info['destination'], $fname, 400); } $fileKey = $file; $file = "{$info['destination']}/$fname"; $fields['anexo'][$fileKey] = self::sendToAmazon($file, $extension, $dir); unlink($file); } } if (empty($fields['anexo'])) { return null; } if (count($fields['anexo']) == 1) { $fields['anexo'] = $fields['anexo'][$fileKey ?? 0]; } $fields['arquivo'] = $fields['anexo']; $fields['url'] = $fields['anexo']; return $fields; } public static function download($file, $dir) { $info = pathinfo($file); $siteKey = Yuppie_Auth::getSiteKey(); $s3 = self::getS3(); $result = $s3->getObject("{$siteKey}/$dir/" . $info['basename']); header('Content-Disposition: attachment; filename=' . $info['basename']); header('Content-Type: ' . $result['ContentType']); echo $result['Body']; die(); } public static function getRawData($url, $dir) { $info = pathinfo($url); $siteKey = Yuppie_Auth::getSiteKey(); $s3 = self::getS3(); $result = $s3->getObject("{$siteKey}/$dir/" . $info['basename']); return $result['Body']; } public static function getSignedUrl($url, $dir) { $info = pathinfo($url); $siteKey = Yuppie_Auth::getSiteKey(); $s3 = self::getS3(); return $s3->getSignedUrl("{$siteKey}/$dir/" . $info['basename'], "+10 minutes"); } public static function downloadFromUrl($file) { $info = pathinfo($file); $s3 = self::getS3(); $result = $s3->getObject($file); header('Content-Disposition: attachment; filename=' . $info['basename']); header('Content-Type: ' . $result['ContentType']); echo $result['Body']; die(); } public static function getSizeBucket() { $siteKey = Yuppie_Auth::getSiteKey(); $s3 = self::getS3(); $objectsListResponse = $s3->getObjectsByBucket($siteKey); $totalSize = 0; $totalItems = 0; foreach( $objectsListResponse as $object ){ $totalSize += $object['Size']; $totalItems++; } //return GB return [ 'total_size' => $totalSize ? round(($totalSize / 1024 / 1024 / 1024), 2) : 0, 'total_items' => $totalItems ]; } public static function checkStorageLimit() { $config = self::getConfigS3(); if (!empty($config['limit_storage']) && $config['limit_storage'] === "unlimited") { return [ 'total_size' => 0, 'total_items' => 0 ]; } $bucketSize = self::getSizeBucket(); $limit = $config['limit_storage'] ?? 10; // 10GB default if( $bucketSize['total_size'] >= $limit ){ throw new DomainException('Você excedeu o seu limite de armazenamento.'); } return true; } public static function deleteObject($file, $dir) { $info = pathinfo($file); $siteKey = Yuppie_Auth::getSiteKey(); $s3 = self::getS3(); return $s3->deleteObject("{$siteKey}/$dir/" . $info['basename']); } }
💾 保存文件
← 返回文件管理器