123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace common\modules\attachment\models;
- use common\modules\attachment\components\UploadedFile;
- use Yii;
- use yii\behaviors\BlameableBehavior;
- use yii\behaviors\TimestampBehavior;
- use yii\helpers\FileHelper;
- use yii\helpers\StringHelper;
- /**
- * This is the model class for table '{{%attachment}}'.
- *
- * @property integer $id
- * @property integer $user_id
- * @property string $name
- * @property string $title
- * @property string $path
- * @property string $extension
- * @property string $description
- * @property string $hash
- * @property integer $size
- * @property string $type
- * @property integer $created_at
- * @property integer $updated_at
- * @property string $url
- */
- class Attachment extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%attachment}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['path', 'hash'], 'required'],
- [['user_id', 'size'], 'integer'],
- [['name', 'title', 'description', 'type', 'extension'], 'string', 'max' => 255],
- [['hash'], 'string', 'max' => 64],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => 'User ID',
- 'name' => 'Name',
- 'title' => 'Title',
- 'description' => 'Description',
- 'hash' => 'Hash',
- 'size' => 'Size',
- 'type' => 'Type',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function behaviors()
- {
- return [
- TimestampBehavior::className(),
- [
- 'class' => BlameableBehavior::className(),
- 'createdByAttribute' => 'user_id',
- 'updatedByAttribute' => false
- ]
- ];
- }
- public function fields()
- {
- return array_merge(parent::fields(), [
- 'url',
- 'isVideo' => function ($model) {
- if (false !== strpos($this->type, 'video')) {
- return true;
- }
- return false;
- },
- 'isImage' => function ($model) {
- if (false !== strpos($this->type, 'image')) {
- return true;
- }
- return false;
- },
- ]);
- }
- /**
- * @return string
- */
- public function getUrl()
- {
- return Yii::$app->storage->getUrl($this->path);
- }
- public function getThumb($width, $height, $options = [])
- {
- $width = (int)$width;
- $height = (int)$height;
- return Yii::$app->storage->thumbnail($this->path, $width, $height);
- }
- public function afterDelete()
- {
- parent::afterDelete();
- // 文件删了
- if (Yii::$app->storage->has($this->path)) {
- Yii::$app->storage->delete($this->path);
- }
- }
- /**
- * @param $hash
- * @return static|null
- */
- public static function findByHash($hash)
- {
- return static::findOne(['hash' => $hash]);
- }
- /**
- * @param $path
- * @param $file UploadedFile
- * @return Attachment|null|static
- * @throws \Exception
- */
- public static function uploadFromPost($path, $file)
- {
- $hash = md5_file($file->tempName);
- $attachment = static::findByHash($hash);
- // if (empty($attachment)) {
- if ($file->extension) {
- // 用hash当文件名,方便根据文件名查找
- $file->name = $hash . '.' . $file->extension;
- }
- // p($file->tempName);
- if ($filePath = Yii::$app->storage->putFile($path, $file)) {
- $attachment = new static();
- $attachment->path = $filePath;
- $attachment->name = $file->hashName;
- $attachment->extension = $file->extension;
- $attachment->type = $file->type;
- $attachment->size = $file->size;
- $attachment->hash = $hash;
- $attachment->save();
- } else {
- throw new \Exception('上传失败');
- }
- // }
- return $attachment;
- }
- /**
- * 抓取远程图片
- * @param $url
- * @return array(Attachment|null|static, string|null)
- */
- public static function uploadFromUrl($path, $url)
- {
- $hash = md5(file_get_contents($url));
- $attachment = static::findByHash($hash);
- $tempFile = Yii::getAlias('@storagePath/upload/' . $hash);
- file_put_contents($tempFile, file_get_contents($url));
- $mimeType = FileHelper::getMimeType($tempFile);
- $extension = current(FileHelper::getExtensionsByMimeType($mimeType, '@common/helpers/mimeTypes.php'));
- if (empty($attachment)) {
- $fileName = $hash . '.' . $extension;
- $filePath = ($path ? ($path . '/') : '') . $fileName;
- $fileSize = filesize($tempFile);
- if (Yii::$app->storage->put($filePath, file_get_contents($tempFile))) {
- @unlink($tempFile);
- $attachment = new static();
- $attachment->path = $filePath;
- $attachment->name = $fileName;
- $attachment->extension = $extension;
- $attachment->type = $mimeType;
- $attachment->size = $fileSize;
- $attachment->hash = $hash;
- $attachment->save();
- } else {
- return [null, '上传失败'];
- }
- }
- return [$attachment, null];
- }
- /**
- * 上传stream二进制数据流文件
- * @param $url
- * @return array(Attachment|null|static, string|null)
- */
- public static function uploadFromStream($stream, $path = null)
- {
- if ($path == null) {
- $path = date('Ymd');
- }
- $hash = md5($stream);
- $attachment = static::findByHash($hash);
- $tempFile = Yii::getAlias('@storagePath/upload/' . $hash);
- file_put_contents($tempFile, $stream);
- $mimeType = FileHelper::getMimeType($tempFile);
- if (!$mimeType) {
- $mimeType = 'image/jpeg';
- $extension = 'jpg';
- } else {
- $extension = current(FileHelper::getExtensionsByMimeType($mimeType, '@common/helpers/mimeTypes.php'));
- if (!$extension || empty($extension)) {
- $mimeType = 'image/jpeg';
- $extension = 'jpg';
- }
- }
- if (empty($attachment)) {
- $fileName = $hash . '.' . $extension;
- $filePath = ($path ? ($path . '/') : '') . $fileName;
- $fileSize = filesize($tempFile);
- if (Yii::$app->storage->put($filePath, file_get_contents($tempFile))) {
- @unlink($tempFile);
- $attachment = new static();
- $attachment->path = $filePath;
- $attachment->name = $fileName;
- $attachment->extension = $extension;
- $attachment->type = $mimeType;
- $attachment->size = $fileSize;
- $attachment->hash = $hash;
- $attachment->save(false);
- } else {
- return [null, '上传失败'];
- }
- }
- return [$attachment, null];
- }
- /**
- * 从临时文件上传
- * @param $path
- * @param $tempFile
- * @return array
- * @throws \yii\base\InvalidConfigException
- * @author nodelog
- */
- public static function uploadFromFile($path, $tempFile)
- {
- $hash = md5(file_get_contents($tempFile));
- $attachment = static::findByHash($hash);
- $mimeType = FileHelper::getMimeType($tempFile);
- $extension = current(FileHelper::getExtensionsByMimeType($mimeType, '@common/helpers/mimeTypes.php'));
- if (empty($attachment)) {
- $fileName = $hash . '.' . $extension;
- $filePath = ($path ? ($path . '/') : '') . $fileName;
- $fileSize = filesize($tempFile);
- if (Yii::$app->storage->put($filePath, file_get_contents($tempFile))) {
- @unlink($tempFile);
- $attachment = new static();
- $attachment->path = $filePath;
- $attachment->name = $fileName;
- $attachment->extension = $extension;
- $attachment->type = $mimeType;
- $attachment->size = $fileSize;
- $attachment->hash = $hash;
- $attachment->save();
- } else {
- return [null, '上传失败'];
- }
- }
- return [$attachment, null];
- }
- public function makeCropStorage($width, $height, $x, $y)
- {
- $url = Yii::$app->storage->crop($this->path, $width, $height, [$x, $y]);
- return self::uploadFromUrl($this->path, $url);
- }
- public function __toString()
- {
- return $this->getUrl();
- }
- }
|