Exemplo n.º 1
0
 public function saveAvatar($avatar)
 {
     if ($this->avatarImage) {
         $this->avatarImage->delete();
     }
     $attachment = new Attachment();
     $attachment->uploadFromFile($avatar);
     $attachment->save();
     $attachmentIndex = new AttachmentIndex();
     $attachmentIndex->attachment_id = $attachment->primaryKey;
     $attachmentIndex->entity = get_class($this);
     $attachmentIndex->entity_id = $this->primaryKey;
     $attachmentIndex->attribute = "avatarImage";
     $attachmentIndex->save();
 }
 /**
  * 先检查文件是否被使用没有使用则删除,在beforedelete中
  * 尝试删除文件.
  *
  * @param unknown $id
  */
 public function deleteFile($id)
 {
     $isUse = AttachmentIndex::find()->where(['attachment_id' => $id])->count();
     if ($isUse > 0) {
         return false;
     }
     $file = Attachment::findOne(['attachment_id' => $id]);
     $file->delete();
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $attachments = Attachment::findAll(["author_id" => $this->authorId]);
     $list = [];
     foreach ($attachments as $attachment) {
         $mediaItem = MediaItem::createFromAttachment($attachment);
         if ($mediaItem->getFileType() === MediaItem::FILE_TYPE_IMAGE) {
             $list[] = ['title' => $attachment->title, 'thumb' => $attachment->getUrl(), 'image' => $attachment->getUrl()];
         } elseif ($mediaItem->getFileType() === MediaItem::FILE_TYPE_DOCUMENT) {
             $list[] = ['title' => $attachment->title, 'name' => $attachment->title, 'link' => $attachment->getUrl(), 'size' => $attachment->size];
         } else {
             $list[] = $url;
         }
     }
     return $list;
 }
Exemplo n.º 4
0
 /**
  * Finds the Attachment model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $id
  * @return Attachment the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Attachment::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 5
0
 /**
  * @return array
  * @throws \HttpException
  */
 public function run()
 {
     $result = [];
     $uploadedFiles = UploadedFile::getInstancesByName($this->fileparam);
     foreach ($uploadedFiles as $uploadedFile) {
         /* @var \yii\web\UploadedFile $uploadedFile */
         $output = [];
         $output['id'] = -1;
         $output[$this->responseUrlParam] = "";
         $output['thumbnailUrl'] = "";
         $output['deleteUrl'] = "";
         $output['updateUrl'] = "";
         $output['path'] = "";
         if ($uploadedFile->error === UPLOAD_ERR_OK) {
             $validationModel = DynamicModel::validateData(['file' => $uploadedFile], $this->validationRules);
             if (!$validationModel->hasErrors()) {
                 $attachment = new Attachment();
                 if ($this->storageLocation == static::STORAGE_LOCATION_TEMPPATH) {
                     $attachment->setStorageDirectory($attachment->getTempDirectory());
                     $attachment->uploadFromPost($uploadedFile);
                 } else {
                     if ($this->storageLocation == static::STORAGE_LOCATION_USERPATH_DATABASE) {
                         $attachment->uploadFromPost($uploadedFile);
                         $attachment->save();
                     }
                 }
                 $output = array_merge($output, $attachment->toArray());
                 if ($attachment->primaryKey) {
                     $output["id"] = $attachment->primaryKey;
                 }
                 $output["path"] = $attachment->getPath();
                 $output[$this->responseUrlParam] = $attachment->getUrl();
                 $output["thumbnailUrl"] = $attachment->getUrl();
                 $output["deleteUrl"] = Url::to(array_merge($this->deleteUrl, ['path' => $output["path"], 'id' => $output['id']]));
                 $output["updateUrl"] = Url::to(array_merge($this->updateUrl, ['path' => $output["path"], 'id' => $output['id']]));
             } else {
                 $output['error'] = true;
                 $output['errors'] = $validationModel->errors;
             }
         } else {
             $output['error'] = true;
             $output['errors'] = $this->resolveErrorMessage($uploadedFile->error);
         }
         $result["files"][] = $output;
     }
     $result = $this->multiple ? $result : array_shift($result);
     return $result;
 }
Exemplo n.º 6
0
 public function getLogoUrl()
 {
     $attachment = Attachment::find()->where([AttachmentIndex::tableName() . '.entity' => get_class($this), AttachmentIndex::tableName() . '.entity_id' => 0, AttachmentIndex::tableName() . '.attribute' => "appLogoImage"])->leftJoin(AttachmentIndex::tableName(), AttachmentIndex::tableName() . '.attachment_id =' . Attachment::tableName() . ".attachment_id")->one();
     if ($attachment) {
         return $attachment->getUrl();
     }
     return null;
 }
Exemplo n.º 7
0
 /**
  *
  * @param Attachment $attachment
  */
 public static function createFromAttachment($attachment)
 {
     $file = $attachment->getFile();
     $item = new static($attachment->getAbsolutePath(), $file->getSize(), $file->getTimestamp(), $file->getType(), $attachment->getUrl());
     return $item;
 }
Exemplo n.º 8
0
 /**
  * 根据url检查该附件是否存在与数据库中,以及该附件的相关信息
  * 外部的src返回null,无效的url返回null
  * @param unknown $src
  */
 public function checkUploadImg($src)
 {
     $pathParts = pathinfo($src);
     $hash = $pathParts['filename'];
     $attachment = Attachment::find()->where(["hash" => $hash])->one();
     $file = [];
     if (!$attachment) {
         $path = \Yii::$app->get("fileStorage")->urlToPath($src);
         if (\Yii::$app->get("fileStorage")->has($path) && ($media = \Yii::$app->get("fileStorage")->get($path))) {
             $file["id"] = -1;
             $file["path"] = $path;
             $file['url'] = $src;
             $file['name'] = $hash;
             $file['hash'] = $hash;
             $file['type'] = $media->getMimetype();
             $file['size'] = $media->getSize();
             $file['extension'] = $pathParts['extension'];
             return $file;
         }
         return null;
     }
     $file = $attachment->getAttributes();
     $file['id'] = $attachment->primaryKey;
     $file['path'] = $attachment->getPath();
     $file['url'] = $attachment->getUrl();
     return $file;
 }
Exemplo n.º 9
0
 public function getAttachments()
 {
     return $this->owner->hasMany(Attachment::className(), ['attachment_id' => 'attachment_id'])->via("attachmentIndex");
 }
Exemplo n.º 10
0
 /**
  *
  * @param \hass\attachment\models\Attachment $attachment
  */
 protected function formartAttachment($attachment)
 {
     if ($attachment instanceof \hass\attachment\models\Attachment) {
         $result = $attachment->toArray();
         $result['id'] = $attachment->primaryKey;
         $result['path'] = $attachment->getPath();
         $result['url'] = $attachment->getUrl();
         $result['thumbnailUrl'] = $result['url'];
         $result['deleteUrl'] = Url::to(array_merge($this->deleteUrl, ["path" => $result['path'], 'id' => $result['id']]));
         $result['deleteType'] = "DELETE";
         return $result;
     } else {
         if (is_string($attachment) && !empty($attachment)) {
             return ["url" => $attachment, "path" => $attachment];
         } else {
             if (is_array($attachment)) {
                 return $attachment;
             }
         }
     }
     return null;
 }