Skip to content

Commit

Permalink
pkp/pkp-lib#7131 Refactor file service to use Laravel container
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWr committed Jun 15, 2021
1 parent 52960f0 commit f830454
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
16 changes: 6 additions & 10 deletions classes/install/Upgrade.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@
use PKP\file\FileManager;
use PKP\identity\Identity;
use PKP\install\Installer;
use PKP\db\DAORegistry;
use PKP\security\Role;

use APP\file\PublicFileManager;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\submission\SubmissionFile;

class Upgrade extends Installer
Expand Down Expand Up @@ -475,10 +471,10 @@ public function moveReviewerFiles()
);
$wrongFilePath = $submissionDir . '/' . $this->_fileStageToPath($revisionRow->file_stage) . '/' . $wrongFilename;
$newFilePath = $submissionDir . '/' . $this->_fileStageToPath(SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT) . '/' . $newFilename;
if (Services::get('file')->fs->has($newFilePath)) {
if ($this->fileService->fs->has($newFilePath)) {
continue;
}
if (!Services::get('file')->fs->rename($wrongFilePath, $newFilePath)) {
if (!$this->fileService->fs->rename($wrongFilePath, $newFilePath)) {
error_log("Unable to move \"${wrongFilePath}\" to \"${newFilePath}\".");
}
}
Expand Down Expand Up @@ -706,10 +702,10 @@ public function fixGenreIdInFileNames()
$timestamp = date('Ymd', strtotime($row->date_uploaded));
$wrongFileName = $submissionId . '-' . '1' . '-' . $row->file_id . '-' . $row->revision . '-' . $row->file_stage . '-' . $timestamp . '.' . strtolower_codesafe($fileManager->parseFileExtension($row->original_file_name));
$sourceFilename = $submissionDir . '/' . $this->_fileStageToPath($row->file_stage) . '/' . $wrongFileName;
if (Services::get('file')->fs->has($targetFilename)) {
if ($this->fileService->fs->has($targetFilename)) {
continue;
}
if (!Services::get('file')->fs->rename($sourceFilename, $targetFilename)) {
if (!$this->fileService->fs->rename($sourceFilename, $targetFilename)) {
error_log("Unable to move \"${sourceFilename}\" to \"${targetFilename}\".");
}
}
Expand Down Expand Up @@ -755,7 +751,7 @@ public function moveCSSFiles()
$submissionDir = Services::get('submissionFile')->getSubmissionDir($journal->getId(), $row->submission_id);
$sourceFilename = $submissionDir . '/public' . '/' . $wrongServerName;
$targetFilename = $submissionDir . '/submission/proof' . '/' . $newServerName;
if (!Services::get('file')->fs->rename($sourceFilename, $targetFilename)) {
if (!$this->fileService->fs->rename($sourceFilename, $targetFilename)) {
error_log("Unable to move \"${sourceFilename}\" to \"${targetFilename}\".");
}
}
Expand Down Expand Up @@ -805,7 +801,7 @@ public function repairSuppFilesFilestage()
);
$oldFileName = $submissionDir . '/' . $submissionFileRevision->_fileStageToPath($submissionFileRevision->getFileStage()) . '/' . $generatedOldFilename;
$newFileName = $submissionDir . '/' . $submissionFileRevision->_fileStageToPath($submissionFileRevision->getFileStage()) . '/' . $generatedNewFilename;
if (!Services::get('file')->fs->rename($oldFileName, $newFileName)) {
if (!$this->fileService->fs->rename($oldFileName, $newFileName)) {
error_log("Unable to move \"${oldFileName}\" to \"${newFileName}\".");
}
DB::table('submission_files')
Expand Down
12 changes: 5 additions & 7 deletions classes/services/OJSServiceProvider.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

namespace APP\services;

use Illuminate\Support\Facades\App;
use Pimple\Container;

use PKP\core\FileServiceProvider;
use PKP\services\PKPAuthorService;
use PKP\services\PKPEmailTemplateService;
use PKP\services\PKPFileService;
use PKP\services\PKPSchemaService;
use PKP\services\PKPSiteService;
use PKP\services\PKPUserService;
Expand All @@ -38,11 +39,6 @@ public function register(Container $pimple)
return new PKPAuthorService();
};

// File service
$pimple['file'] = function () {
return new PKPFileService();
};

// Issue service
$pimple['issue'] = function () {
return new IssueService();
Expand Down Expand Up @@ -80,7 +76,9 @@ public function register(Container $pimple)

// Submission file service
$pimple['submissionFile'] = function () {
return new SubmissionFileService();
return new SubmissionFileService(
App::make(FileServiceProvider::class)
);
};

// Email Templates service
Expand Down
6 changes: 4 additions & 2 deletions classes/services/SubmissionFileService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
namespace APP\services;

use APP\core\Application;
use PKP\core\FileServiceProvider;
use PKP\db\DAORegistry;
use PKP\plugins\HookRegistry;

use PKP\plugins\HookRegistry;
use PKP\search\SubmissionSearch;

class SubmissionFileService extends \PKP\services\PKPSubmissionFileService
{
/**
* Initialize hooks for extending PKPSubmissionFileService
*/
public function __construct()
public function __construct(FileServiceProvider $fileService)
{
parent::__construct($fileService);
HookRegistry::register('SubmissionFile::delete::before', [$this, 'deleteSubmissionFile']);
}

Expand Down
9 changes: 6 additions & 3 deletions pages/article/ArticleHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use APP\submission\Submission;
use APP\template\TemplateManager;
use Firebase\JWT\JWT;
use Illuminate\Support\Facades\App;
use PKP\core\FileServiceProvider;
use PKP\security\authorization\ContextRequiredPolicy;
use PKP\submission\PKPSubmission;

Expand Down Expand Up @@ -456,16 +458,17 @@ public function download($args, $request)

if (!HookRegistry::call('ArticleHandler::download', [$this->article, &$this->galley, &$this->fileId])) {
$submissionFile = Services::get('submissionFile')->get($this->fileId);
$fileService = App::make(FileServiceProvider::class);

if (!Services::get('file')->fs->has($submissionFile->getData('path'))) {
if (!$fileService->fs->has($submissionFile->getData('path'))) {
$request->getDispatcher()->handle404();
}

$filename = Services::get('file')->formatFilename($submissionFile->getData('path'), $submissionFile->getLocalizedData('name'));
$filename = $fileService->formatFilename($submissionFile->getData('path'), $submissionFile->getLocalizedData('name'));

$returner = true;
HookRegistry::call('FileManager::downloadFileFinished', [&$returner]);
Services::get('file')->download($submissionFile->getData('fileId'), $filename);
$fileService->download($submissionFile->getData('fileId'), $filename);
}
} else {
header('HTTP/1.0 403 Forbidden');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
* @brief Class for HtmlArticleGalley plugin
*/

use PKP\submission\SubmissionFile;
use PKP\plugins\HookRegistry;
import('lib.pkp.classes.plugins.GenericPlugin');

use APP\facades\Repo;
use APP\file\PublicFileManager;

import('lib.pkp.classes.plugins.GenericPlugin');

use APP\template\TemplateManager;
use Illuminate\Support\Facades\App;

use PKP\core\FileServiceProvider;
use PKP\plugins\HookRegistry;
use PKP\submission\SubmissionFile;

class HtmlArticleGalleyPlugin extends \PKP\plugins\GenericPlugin
{
Expand Down Expand Up @@ -153,7 +156,8 @@ protected function _getHTMLContents($request, $galley)
{
$submissionFile = $galley->getFile();
$submissionId = $submissionFile->getData('submissionId');
$contents = Services::get('file')->fs->read($submissionFile->getData('path'));
$fileService = App::make(FileServiceProvider::class);
$contents = $fileService->fs->read($submissionFile->getData('path'));

// Replace media file references
$embeddableFilesIterator = Services::get('submissionFile')->getMany([
Expand Down
13 changes: 9 additions & 4 deletions plugins/importexport/datacite/filter/DataciteXmlFilter.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
* @brief Class that converts an Issue to a DataCite XML document.
*/

use APP\article\ArticleGalley;
use APP\facades\Repo;
use APP\issue\Issue;
use APP\submission\Submission;
use APP\workflow\EditorDecisionActionsManager;
use APP\issue\Issue;
use APP\article\ArticleGalley;
use Illuminate\Support\Facades\App;
use PKP\core\FileServiceProvider;

// Title types
define('DATACITE_TITLETYPE_TRANSLATED', 'TranslatedTitle');
Expand Down Expand Up @@ -54,6 +56,8 @@

class DataciteXmlFilter extends NativeExportFilter
{
protected FileServiceProvider $fileService;

/**
* Constructor
*
Expand All @@ -63,6 +67,7 @@ public function __construct($filterGroup)
{
$this->setDisplayName('DataCite XML export');
parent::__construct($filterGroup);
$this->fileService = App::make(FileServiceProvider::class);
}

//
Expand Down Expand Up @@ -635,8 +640,8 @@ public function createSizesNode($doc, $issue, $article, $publication, $galley, $
// The galley represents the article.
$pages = $publication->getData('pages');
$path = $galleyFile->getData('path');
$size = Services::get('file')->fs->getSize($path);
$sizes[] = Services::get('file')->getNiceFileSize($size);
$size = $this->fileService->fs->getSize($path);
$sizes[] = $this->fileService->getNiceFileSize($size);
break;
case isset($article):
$pages = $publication->getData('pages');
Expand Down

0 comments on commit f830454

Please sign in to comment.