Ejemplo n.º 1
0
 public function uploadFile($fileName, $fileType, $fileSize, $file, $userId, $currentId)
 {
     $userFile = UserFile::findOne(['md5' => md5_file($file)]);
     if ($userFile == null) {
         $userFile = new UserFile();
         $userFile->filename = $fileName;
         $userFile->filetype = $fileType;
         $userFile->filesize = $fileSize;
         $userFile->file = $file;
         if (!$userFile->save()) {
             return '2';
         }
     }
     $fm_log = FileManageLog::findAll(['file_id' => $userFile->_id]);
     if ($fm_log != null) {
         return '3';
         //违规文件
     }
     $disk = Disk::findOne(['user_id' => $userId]);
     try {
         $tran = \Yii::$app->db->beginTransaction();
         $created_date = date('Y-m-d H:i:s');
         $user_id = $userId;
         $record_id = md5($user_id . $fileName . $created_date);
         $fileRecord = new FileRecord();
         $fileRecord->f_record_id = $record_id;
         $fileRecord->f_record_type = "1";
         $fileRecord->file_id = $userFile->_id;
         $fileRecord->user_id = $user_id;
         $fileRecord->file_name = $fileName;
         $tmps = explode('.', $fileName);
         if (count($tmps) == 1) {
             $fileRecord->extension = '';
         } else {
             $fileRecord->extension = end($tmps);
         }
         $fileRecord->file_type = $fileType;
         $fileRecord->file_size = $fileSize;
         $fileRecord->parent_id = $currentId;
         $fileRecord->upload_date = $created_date;
         $fileRecord->state = "0";
         if ($fileRecord->save()) {
             $disk->available_size = $disk->available_size - $fileSize;
             if ($disk->available_size < 0) {
                 return '1';
                 //空间不足
                 $tran->rollBack();
             }
             if ($disk->save()) {
                 $parent_folder = FileRecord::findOne(['f_record_id' => $currentId]);
                 while ($parent_folder->parent_id != '0') {
                     $parent_folder->file_size = $parent_folder->file_size + $fileSize;
                     if ($parent_folder->save()) {
                         $parent_folder = FileRecord::findOne(['f_record_id' => $parent_folder->parent_id]);
                     } else {
                         $tran->rollBack();
                         return 'error';
                     }
                 }
                 $tran->commit();
                 return FileRecord::find()->where(['f_record_id' => $record_id])->asArray()->one();
             }
         } else {
             return '2';
             //数据库错误
         }
     } catch (Exception $e) {
         return '2';
     }
 }
Ejemplo n.º 2
0
 public function logManageFile($fileId, $info)
 {
     $tran = \Yii::$app->db->beginTransaction();
     $records = FileRecord::findAll(['file_id' => $fileId]);
     $fileService = new FileService();
     foreach ($records as $record) {
         if ($record->state == '0') {
             //$fileService->deleteFile($record->f_record_id,$record->user_id);
         }
     }
     $conn = \Yii::$app->db;
     $sql = 'update file_record set state="2" where file_id="' . $fileId . '"';
     $command = $conn->createCommand($sql);
     if (!$command->execute()) {
         $tran->rollBack();
         return 'error';
     }
     $log = new FileManageLog();
     $log->fm_log_id = md5($fileId . $_SESSION['admin']['admin_id'] . date('Y-m-d H:i:s'));
     $log->file_id = $fileId;
     $log->admin_id = $_SESSION['admin']['admin_id'];
     $log->fm_manage_type = '1';
     //禁用文件
     $log->fm_manage_info = $info;
     $log->create_date = date('Y-m-d H:i:s');
     if ($log->save()) {
         $tran->commit();
         return 'success';
     } else {
         $tran->rollBack();
         return $log->errors;
     }
 }