/**
  * Action to upload multiple files.
  * @return multitype:boolean multitype:
  */
 public function actionIndex()
 {
     Yii::$app->response->format = 'json';
     if (!$this->canWrite()) {
         throw new HttpException(401, Yii::t('CfilesModule.base', 'Insufficient rights to execute this action.'));
     }
     $response = [];
     foreach (UploadedFile::getInstancesByName('files') as $cFile) {
         $folder = $this->getCurrentFolder();
         $currentFolderId = empty($folder) ? self::ROOT_ID : $folder->id;
         // check if the file already exists in this dir
         $filesQuery = File::find()->contentContainer($this->contentContainer)->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($cFile->name), 'parent_folder_id' => $currentFolderId]);
         $file = $filesQuery->one();
         // if not, initialize new File
         if (empty($file)) {
             $file = new File();
             $humhubFile = new \humhub\modules\file\models\File();
         } else {
             $humhubFile = $file->baseFile;
             // logging file replacement
             $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]);
             $response['log'] = true;
         }
         $humhubFile->setUploadedFile($cFile);
         if ($humhubFile->validate()) {
             $file->content->container = $this->contentContainer;
             $folder = $this->getCurrentFolder();
             if ($folder !== null) {
                 $file->parent_folder_id = $folder->id;
             }
             if ($file->save()) {
                 $humhubFile->object_model = $file->className();
                 $humhubFile->object_id = $file->id;
                 $humhubFile->save();
                 $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]);
             } else {
                 $count = 0;
                 $messages = "";
                 // show multiple occurred errors
                 foreach ($file->errors as $key => $message) {
                     $messages .= ($count++ ? ' | ' : '') . $message[0];
                 }
                 $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages;
                 $response['log'] = true;
             }
         } else {
             $count = 0;
             $messages = "";
             // show multiple occurred errors
             foreach ($humhubFile->errors as $key => $message) {
                 $messages .= ($count++ ? ' | ' : '') . $message[0];
             }
             $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages;
             $response['log'] = true;
         }
     }
     $response['files'] = $this->files;
     return $response;
 }
 /**
  * Create a cfile model and create and connect it with its basefile File model from a given data file, connect it with its parent folder.
  * TODO: This method has a lot in common with BrowseController/actionUpload, common logic needs to be extracted and reused
  *
  * @param Response $response
  *            the response errors will be parsed to.
  * @param int $parentFolderId
  *            the files pid.
  * @param string $folderPath
  *            the path of the folder the file data lies in.
  * @param string $filename
  *            the files name.
  */
 protected function generateModelFromFile(&$response, $parentFolderId, $folderPath, $filename)
 {
     $filepath = $folderPath . DIRECTORY_SEPARATOR . $filename;
     // check if the file already exists in this dir
     $filesQuery = File::find()->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($filename), 'parent_folder_id' => $parentFolderId]);
     $file = $filesQuery->one();
     // if not, initialize new File
     if (empty($file)) {
         $file = new File();
         $humhubFile = new \humhub\modules\file\models\File();
     } else {
         // else replace the existing file
         $humhubFile = $file->baseFile;
         // logging file replacement
         $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]);
         $response['log'] = true;
     }
     // populate the file
     $humhubFile->mime_type = FileHelper::getMimeType($filepath);
     if ($humhubFile->mime_type == 'image/jpeg') {
         ImageConverter::TransformToJpeg($filepath, $filepath);
     }
     $humhubFile->size = filesize($filepath);
     $humhubFile->file_name = $filename;
     $humhubFile->newFileContent = stream_get_contents(fopen($filepath, 'r'));
     if ($humhubFile->save()) {
         $file->content->container = $this->contentContainer;
         $file->parent_folder_id = $parentFolderId;
         if ($file->save()) {
             $humhubFile->object_model = $file->className();
             $humhubFile->object_id = $file->id;
             $humhubFile->save();
             $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]);
         } else {
             $count = 0;
             $messages = "";
             // show multiple occurred errors
             foreach ($file->errors as $key => $message) {
                 $messages .= ($count++ ? ' | ' : '') . $message[0];
             }
             $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages;
             $response['log'] = true;
         }
     } else {
         $count = 0;
         $messages = "";
         // show multiple occurred errors
         foreach ($humhubFile->errors as $key => $message) {
             $messages .= ($count++ ? ' | ' : '') . $message[0];
         }
         $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages;
         $response['log'] = true;
     }
 }