From c3d3899e3227cee358aac175532873e002934c03 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 24 Feb 2023 14:09:02 +0900 Subject: [PATCH 01/16] =?UTF-8?q?test:=20Github=20Action=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9501566..7cfa881 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,6 +44,7 @@ jobs: MYSQL_VERSION: ${{ matrix.mysql }} MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: cakephp_test + COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v2 From eb496904c2db26cb338ffa25b2840f0f3c711176 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Sun, 26 Feb 2023 00:22:08 +0900 Subject: [PATCH 02/16] change: Version number to 3.3.6 --- VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION.txt b/VERSION.txt index fa7adc7..9c25013 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -3.3.5 +3.3.6 From b60d8d5aa0ceeb32b0d40af979652c7b4e98831d Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Sat, 25 Mar 2023 15:49:51 +0900 Subject: [PATCH 03/16] =?UTF-8?q?fix:=20=E3=82=AD=E3=83=A3=E3=83=93?= =?UTF-8?q?=E3=83=8D=E3=83=83=E3=83=88=E3=81=A7=E6=97=A5=E6=9C=AC=E8=AA=9E?= =?UTF-8?q?=E3=81=AEZIP=E3=82=92=E8=A7=A3=E5=87=8D=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=81=A8=E6=96=87=E5=AD=97=E5=8C=96=E3=81=91=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=83=90=E3=82=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utility/UnZip.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Utility/UnZip.php b/Utility/UnZip.php index f37bfe6..e1ddb0d 100644 --- a/Utility/UnZip.php +++ b/Utility/UnZip.php @@ -126,7 +126,7 @@ protected function _extractWithZipArchiveClass($path) { $zip->setPassword($this->_password); } $index = 0; - while ($zipEntry = $zip->statIndex($index)) { + while ($zipEntry = $zip->statIndex($index, ZipArchive::FL_ENC_RAW)) { $zipEntryName = $zipEntry['name']; $destName = mb_convert_encoding($zipEntry['name'], $encodeCharset, 'auto'); if ($zip->renameName($zipEntryName, $destName) === false) { From 5bc325c6b56e3edda559fb1c7f9b8b07294d0107 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 10 May 2023 21:59:08 +0900 Subject: [PATCH 04/16] =?UTF-8?q?fix:=20Ubuntu=E3=81=8A=E3=82=88=E3=81=B3R?= =?UTF-8?q?edHat=208=E7=B3=BB=E7=B5=B1=E3=81=AELinux=E3=83=87=E3=82=A3?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=83=AA=E3=83=93=E3=83=A5=E3=83=BC=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=81=AEPHP7=E3=81=A7=E5=86=85=E9=83=A8?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=83=90=E3=82=B0=E4=BF=AE=E6=AD=A3=20https://github.?= =?UTF-8?q?com/NetCommons3/NetCommons3/issues/1703?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utility/UnZip.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Utility/UnZip.php b/Utility/UnZip.php index e1ddb0d..90b373b 100644 --- a/Utility/UnZip.php +++ b/Utility/UnZip.php @@ -129,7 +129,7 @@ protected function _extractWithZipArchiveClass($path) { while ($zipEntry = $zip->statIndex($index, ZipArchive::FL_ENC_RAW)) { $zipEntryName = $zipEntry['name']; $destName = mb_convert_encoding($zipEntry['name'], $encodeCharset, 'auto'); - if ($zip->renameName($zipEntryName, $destName) === false) { + if ($zip->renameIndex($index, $destName) === false) { return false; } if ($zip->extractTo($path, $destName) === false) { From ab45dfa5fe6248007c1cc16ceb5fa4dacb850bb2 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 10 May 2023 22:25:58 +0900 Subject: [PATCH 05/16] =?UTF-8?q?test:=20Github=20Action=E3=81=A7=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7cfa881..3b2212b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,7 +15,7 @@ name: tests jobs: setup: name: setup - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Slack Notification on Start uses: rtCamp/action-slack-notify@v2.2.0 @@ -28,7 +28,7 @@ jobs: tests: name: tests needs: setup - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest strategy: matrix: php: [ '7.1', '7.2', '7.3', '7.4' ] @@ -148,7 +148,7 @@ jobs: teardown: name: teardown - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest needs: tests steps: # テスト成功時はこちらのステップが実行される From 206fef9b5e179fabfb8cda36b284b0fa0b286e3f Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 10 May 2023 22:30:00 +0900 Subject: [PATCH 06/16] =?UTF-8?q?test:=20Github=20Action=E3=81=A7=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3b2212b..77f9121 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -92,37 +92,44 @@ jobs: docker-compose exec -T nc3app bash /opt/scripts/app-build.sh - name: phpcs (PHP CodeSniffer) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/phpcs.sh - name: phpmd (PHP Mess Detector) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/phpmd.sh - name: phpcpd (PHP Copy/Paste Detector) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/phpcpd.sh - name: gjslint (JavaScript Style Check) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/gjslint.sh - name: phpdoc (PHP Documentor) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/phpdoc.sh - name: phpunit (PHP UnitTest) + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose exec -T nc3app bash /opt/scripts/phpunit.sh sudo -s chmod a+w -R ${NC3_BUILD_DIR}/build - name: push coveralls + if: success() env: COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} COVERALLS_FLAG_NAME: ${{ matrix.php }} @@ -132,6 +139,7 @@ jobs: vendors/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v - name: docker-compose remove + if: always() run: | cd ${NC3_DOCKER_DIR} docker-compose rm -f From 278d404108589f6eae32aca3f0cdbf9f7cf9176d Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Thu, 11 May 2023 10:04:20 +0900 Subject: [PATCH 07/16] =?UTF-8?q?test:=20Github=20Action=E3=81=A7=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 77f9121..bfe7e70 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -128,15 +128,15 @@ jobs: docker-compose exec -T nc3app bash /opt/scripts/phpunit.sh sudo -s chmod a+w -R ${NC3_BUILD_DIR}/build - - name: push coveralls - if: success() - env: - COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COVERALLS_FLAG_NAME: ${{ matrix.php }} - run: | - cd ${NC3_BUILD_DIR} - ls -la ${NC3_BUILD_DIR} - vendors/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v +# - name: push coveralls +# if: success() +# env: +# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# COVERALLS_FLAG_NAME: ${{ matrix.php }} +# run: | +# cd ${NC3_BUILD_DIR} +# ls -la ${NC3_BUILD_DIR} +# vendors/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v - name: docker-compose remove if: always() From de0bf3ff601a0535e5677a6fab40464db4477864 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 2 Jun 2023 08:53:20 +0900 Subject: [PATCH 08/16] =?UTF-8?q?fix:=20WYSIWYG=E3=81=AE=E3=83=80=E3=82=A6?= =?UTF-8?q?=E3=83=B3=E3=83=AD=E3=83=BC=E3=83=89=E5=87=A6=E7=90=86=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/Component/DownloadComponent.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Controller/Component/DownloadComponent.php b/Controller/Component/DownloadComponent.php index a168350..fcb0c94 100644 --- a/Controller/Component/DownloadComponent.php +++ b/Controller/Component/DownloadComponent.php @@ -80,10 +80,11 @@ public function doDownload($contentId, $options = array()) { * * @param int $uploadFileId UploadFile ID * @param array $options オプション field : ダウンロードのフィールド名, size: nullならオリジナル thumb, small, medium, big + * @param string $pluginKey プラグインキー * @return CakeResponse|null * @throws ForbiddenException */ - public function doDownloadByUploadFileId($uploadFileId, $options = array()) { + public function doDownloadByUploadFileId($uploadFileId, $options = [], $pluginKey = 'wysiwyg') { if (isset($options['size'])) { $size = $options['size']; unset($options['size']); @@ -101,10 +102,11 @@ public function doDownloadByUploadFileId($uploadFileId, $options = array()) { $UploadFile = ClassRegistry::init('Files.UploadFile'); $file = $UploadFile->findById($uploadFileId); - if (! $file) { + if (! $file || $file['UploadFile']['plugin_key'] !== $pluginKey) { //データがない=リンク切れ。リンク切れの場合、ログアウトしないようにするため、メッセージを追加 throw new ForbiddenException('Not found file'); } + return $this->_downloadUploadFile($file, $size, $options); } From 0c0d5021f22dc3be1a9659199ba9909d2c27f291 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 2 Jun 2023 09:58:21 +0900 Subject: [PATCH 09/16] =?UTF-8?q?test:=20UnitTest=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadComponent/DoDownloadByUploadFileIdTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/Case/Controller/Component/DownloadComponent/DoDownloadByUploadFileIdTest.php b/Test/Case/Controller/Component/DownloadComponent/DoDownloadByUploadFileIdTest.php index 589efea..a067a36 100644 --- a/Test/Case/Controller/Component/DownloadComponent/DoDownloadByUploadFileIdTest.php +++ b/Test/Case/Controller/Component/DownloadComponent/DoDownloadByUploadFileIdTest.php @@ -109,7 +109,7 @@ public function testDoDownloadByUploadFileId() { ->will($this->returnValue(true)); $UploadFileMock->uploadBasePath = $uploadBasePath; - $this->controller->Download->doDownloadByUploadFileId($fileId); + $this->controller->Download->doDownloadByUploadFileId($fileId, [], 'site_manager'); } /** @@ -159,7 +159,7 @@ public function testFixAvatarDownloadProblem() { // コンテンツキーが入ってるとブロックガードで例外が発生してた。-> DownloadComponent修正で例外発生しなくなったのを確認 // @codingStandardsIgnoreStart NOTICE無視して例外発生するのを確認したかったので@でエラー抑止してます。 - @$this->controller->Download->doDownloadByUploadFileId($fileId); + @$this->controller->Download->doDownloadByUploadFileId($fileId, [], 'users'); // @codingStandardsIgnoreEnd } } From d089279e2dd4a920ba922afa15320781f431bd43 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 16 Jun 2023 08:18:01 +0900 Subject: [PATCH 10/16] fix: escape --- View/Helper/FilesFormHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/View/Helper/FilesFormHelper.php b/View/Helper/FilesFormHelper.php index c35a19d..465cc6f 100644 --- a/View/Helper/FilesFormHelper.php +++ b/View/Helper/FilesFormHelper.php @@ -109,7 +109,7 @@ public function uploadFile($fieldName, $options = array()) { if (isset($this->_uploadFileNames[$fieldName])) { if ($filename) { - $output .= $this->_uploadFileNames[$fieldName]; + $output .= h($this->_uploadFileNames[$fieldName]); } if ($remove) { $output .= $this->NetCommonsForm->checkbox( From b4ded54d4dca771248718d849736edb1a21cf6c7 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Thu, 5 Oct 2023 18:04:34 +0900 Subject: [PATCH 11/16] =?UTF-8?q?test:=20github=20action=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bfe7e70..6da4321 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,12 +3,10 @@ on: branches: - main - master - - availability pull_request: branches: - main - master - - availability name: tests @@ -19,6 +17,7 @@ jobs: steps: - name: Slack Notification on Start uses: rtCamp/action-slack-notify@v2.2.0 + if: env.SLACK_WEBHOOK != '' env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TESTS }} SLACK_CHANNEL: notify-nc3-tests @@ -128,15 +127,14 @@ jobs: docker-compose exec -T nc3app bash /opt/scripts/phpunit.sh sudo -s chmod a+w -R ${NC3_BUILD_DIR}/build -# - name: push coveralls -# if: success() -# env: -# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} -# COVERALLS_FLAG_NAME: ${{ matrix.php }} -# run: | -# cd ${NC3_BUILD_DIR} -# ls -la ${NC3_BUILD_DIR} -# vendors/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v +# - name: push coveralls +# env: +# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# COVERALLS_FLAG_NAME: ${{ matrix.php }} +# run: | +# cd ${NC3_BUILD_DIR} +# ls -la ${NC3_BUILD_DIR} +# vendors/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v - name: docker-compose remove if: always() @@ -147,7 +145,7 @@ jobs: # テスト失敗時はこちらのステップが実行される - name: Slack Notification on Failure uses: rtCamp/action-slack-notify@v2.2.0 - if: failure() + if: env.SLACK_WEBHOOK != '' && failure() env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TESTS }} SLACK_CHANNEL: notify-nc3-tests @@ -161,8 +159,8 @@ jobs: steps: # テスト成功時はこちらのステップが実行される - name: Slack Notification on Success - if: success() uses: rtCamp/action-slack-notify@v2.2.0 + if: env.SLACK_WEBHOOK != '' && success() env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_TESTS }} SLACK_CHANNEL: notify-nc3-tests From 68f1d281b05c462a5f74fd7a5d035fd5f77f2ea2 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Mon, 9 Oct 2023 18:06:17 +0900 Subject: [PATCH 12/16] change: Version number to 3.3.7 --- VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION.txt b/VERSION.txt index 9c25013..86fb650 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -3.3.6 +3.3.7 From 8d9f472c3bb29c54b1bde2effa7b4b86a3296f2f Mon Sep 17 00:00:00 2001 From: Ryuji AMANO Date: Wed, 6 Mar 2024 13:32:53 +0900 Subject: [PATCH 13/16] =?UTF-8?q?wip:=20=E5=AE=9F=E9=A8=93=E4=B8=AD?= =?UTF-8?q?=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E3=82=B3=E3=83=9F?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=81=97=E3=81=A6=E6=AE=8B=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=8A=E3=81=8F=E3=80=82=EF=BC=88=E5=BE=8C=E6=97=A5=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E4=BA=88=E5=AE=9A=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TemporaryFile/TemporaryFileTest.php | 98 +++++++++++++++++++ .../TemporaryFolder/TemporaryFolderTest.php | 79 ++++++++++++++- Utility/TemporaryFile2.php | 67 +++++++++++++ Utility/TemporaryFolder2.php | 64 ++++++++++++ 4 files changed, 303 insertions(+), 5 deletions(-) create mode 100644 Utility/TemporaryFile2.php create mode 100644 Utility/TemporaryFolder2.php diff --git a/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php b/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php index 9e96734..4a1d9e4 100644 --- a/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php +++ b/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php @@ -9,6 +9,7 @@ App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); App::uses('NetCommonsControllerTestCase', 'NetCommons.TestSuite'); App::uses('TemporaryFile', 'Files.Utility'); +App::uses('TemporaryFile2', 'Files.Utility'); /** * Summary for AttachmentBehavior Test Case @@ -47,4 +48,101 @@ public function testDelete() { $temporaryFile->delete(); $this->assertFalse(file_exists($path)); } + public function testMemoryLeakForKeepFiles() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $files[] = new TemporaryFile(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakForKeepFilesByTemporaryFile2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $files[] = new TemporaryFile2(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithDelete() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $file = new TemporaryFile(); + $files[] =$file; + $file->delete(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithDeleteByTemporaryFile2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $file = new TemporaryFile2(); + $files[] =$file; + $file->delete(); + } + $this->reportMemory('after ', $before); + } + public function testMemoryLeakWithDeleteAndOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFile(); + $folder->delete(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithDeleteAndOverwriteByTemporaryFile2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFile2(); + $folder->delete(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $file = new TemporaryFile(); + // ここでテンポラリファイルを使った処理を想定 + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithOverwriteByTemporaryFile2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $file = new TemporaryFile2(); + // ここでテンポラリファイルを使った処理を想定 + } + $this->reportMemory('after ', $before); + } + + private function reportMemory(string $string, int $before = null) { + $current = memory_get_usage(false); + print($string . ':' . number_format($current)); + if ($before !== null) { + print "\n"; + print($string . '(diff):' . number_format($current - $before)); + } + print "\n"; + return $current; + } } diff --git a/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php b/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php index 55dc695..9d4b007 100644 --- a/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php +++ b/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php @@ -9,6 +9,7 @@ App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); App::uses('NetCommonsControllerTestCase', 'NetCommons.TestSuite'); App::uses('TemporaryFolder', 'Files.Utility'); +App::uses('TemporaryFolder2', 'Files.Utility'); /** * Summary for AttachmentBehavior Test Case @@ -46,11 +47,79 @@ public function testDelete() { $path = $tempFolder->path; $tempFolder->delete(); $this->assertFalse(file_exists($path)); + } + public function testMemoryLeakForKeepFolders() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folders[] = new TemporaryFolder(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakForKeepFoldersByTemporaryFolder2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folders[] = new TemporaryFolder2(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithDelete() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFolder(); + $folders[] =$folder; + $folder->delete(); + } + $this->reportMemory('after ', $before); + } - //デストラクタ廃止したのでこのテストも廃止 - //$tempFolder2 = new TemporaryFolder(); - //$path2 = $tempFolder2->path; - //unset($tempFolder2); - //$this->assertFalse(file_exists($path2)); + public function testMemoryLeakWithDeleteByTemporaryFolder2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFolder2(); + $folders[] =$folder; + $folder->delete(); + } + $this->reportMemory('after ', $before); + } + public function testMemoryLeakWithDeleteAndOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFolder(); + $folder->delete(); + } + $this->reportMemory('after ', $before); + } + + public function testMemoryLeakWithDeleteAndOverwriteByTemporaryFolder2() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->reportMemory('before'); + for ($i = 0; $i< $max; $i++) { + $folder = new TemporaryFolder2(); + $folder->delete(); + } + $this->reportMemory('after ', $before); + } + private function reportMemory(string $string, int $before = null) { + $current = memory_get_usage(false); + print($string . ':' . number_format($current)); + if ($before !== null) { + print "\n"; + print($string . '(diff):' . number_format($current - $before)); + } + print "\n"; + return $current; } } diff --git a/Utility/TemporaryFile2.php b/Utility/TemporaryFile2.php new file mode 100644 index 0000000..1a37b55 --- /dev/null +++ b/Utility/TemporaryFile2.php @@ -0,0 +1,67 @@ + + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + */ + +App::uses('File', 'Utility'); +App::uses('TemporaryFolder', 'Files.Utility'); +App::uses('TemporaryFolder2', 'Files.Utility'); +App::uses('Security', 'Utility'); + +/** + * Class TemporaryFile + */ +class TemporaryFile2 extends File { + + private static $filePaths = []; + private static $isRegisteredShutdownFunction = false; + +/** + * @var TemporaryFolder テンポラリファイルを配置するテンポラリフォルダ + */ + protected $_tmpFolder; + +/** + * TemporaryFile constructor. + * + * @param string $folderPath テンポラリフォルダを作成するフォルダパス。指定されなければテンポラリフォルダを作成する + */ + public function __construct($folderPath = null) { + if ($folderPath === null) { + $this->_tmpFolder = new TemporaryFolder2(); + $folderPath = $this->_tmpFolder->path; + } + $fileName = Security::hash(mt_rand() . microtime(), 'md5'); + + $path = $folderPath . DS . $fileName; + + self::$filePaths[] = $path; + if (!self::$isRegisteredShutdownFunction) { + register_shutdown_function([TemporaryFile2::class, 'deleteAll']); + self::$isRegisteredShutdownFunction = true; + } + parent::__construct($path, true); + } + public function delete() { + $key = array_search($this->path, self::$filePaths); + if ($key !== false) { + unset(self::$filePaths[$key]); + if ($this->_tmpFolder instanceof TemporaryFolder2) { + $this->_tmpFolder->delete(); + } + } + parent::delete(); + } + + public static function deleteAll() { + foreach (self::$filePaths as $path) { + $file = new File($path); + $file->delete(); + } + self::$filePaths = []; + } +} diff --git a/Utility/TemporaryFolder2.php b/Utility/TemporaryFolder2.php new file mode 100644 index 0000000..a9e7e48 --- /dev/null +++ b/Utility/TemporaryFolder2.php @@ -0,0 +1,64 @@ +path パス取得 + * // あれこれ + * $tempFolder->delete(); // 不要になったらdelete + * ``` + */ +class TemporaryFolder2 extends Folder { + + private static $folderPaths = []; + private static $isRegisteredShutdownFunction = false; + +/** + * TemporaryFolder constructor. + */ + public function __construct() { + $path = TMP; + $path .= Security::hash(mt_rand() . microtime(), 'md5'); + //$mode = '0775'; // ε(     v ゚ω゚) <パーミッションいくつが適切だ? + $mode = false; // とりあえずデフォルトのまま + self::$folderPaths[] = $path; + if (!self::$isRegisteredShutdownFunction) { + register_shutdown_function([TemporaryFolder2::class, 'deleteAll']); + self::$isRegisteredShutdownFunction = true; + } + parent::__construct($path, true, $mode); + } + + public function delete($path = null) { + $path = $path ?? $this->path; + if ($path) { + $key = array_search($path, self::$folderPaths); + if ($key !== false) { + unset(self::$folderPaths[$key]); + } + } + parent::delete($path); + } + + public static function deleteAll() { + $folder = new Folder(); + foreach (self::$folderPaths as $path) { + $folder->delete($path); + } + self::$folderPaths = []; + } +} \ No newline at end of file From 11f727606ac1c49696299c3489e0d97d0c1be134 Mon Sep 17 00:00:00 2001 From: Ryuji AMANO Date: Thu, 7 Mar 2024 11:02:48 +0900 Subject: [PATCH 14/16] =?UTF-8?q?fix:=20TemporaryFile,=20TemporaryFolder?= =?UTF-8?q?=E3=81=AE=E3=83=A1=E3=83=A2=E3=83=AA=E3=83=AA=E3=83=BC=E3=82=AF?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Utility/TemporaryFile/MemoryLeakTest.php | 117 ++++++++++++++++++ .../TemporaryFile/TemporaryFileTest.php | 98 --------------- .../TemporaryFolder/MemoryLeakTest.php | 105 ++++++++++++++++ .../TemporaryFolder/TemporaryFolderTest.php | 74 ----------- Utility/TemporaryFile.php | 49 +++++++- Utility/TemporaryFile2.php | 67 ---------- Utility/TemporaryFolder.php | 47 ++++++- Utility/TemporaryFolder2.php | 64 ---------- 8 files changed, 310 insertions(+), 311 deletions(-) create mode 100644 Test/Case/Utility/TemporaryFile/MemoryLeakTest.php create mode 100644 Test/Case/Utility/TemporaryFolder/MemoryLeakTest.php delete mode 100644 Utility/TemporaryFile2.php delete mode 100644 Utility/TemporaryFolder2.php diff --git a/Test/Case/Utility/TemporaryFile/MemoryLeakTest.php b/Test/Case/Utility/TemporaryFile/MemoryLeakTest.php new file mode 100644 index 0000000..262069b --- /dev/null +++ b/Test/Case/Utility/TemporaryFile/MemoryLeakTest.php @@ -0,0 +1,117 @@ +markTestSkipped(); + } + +/** + * testMemoryLeakForKeepFiles + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakForKeepFiles() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $files[] = new TemporaryFile(); + } + $this->__reportMemory('after ', $before); + } + +/** + * testMemoryLeakWithDelete + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakWithDelete() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $file = new TemporaryFile(); + $files[] = $file; + $file->delete(); + } + $this->__reportMemory('after ', $before); + } + +/** + * testMemoryLeakWithDeleteAndOverwrite + * + * @return void + */ + public function testMemoryLeakWithDeleteAndOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $folder = new TemporaryFile(); + $folder->delete(); + } + $this->__reportMemory('after ', $before); + } + +/** + * testMemoryLeakWithOverwrite + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakWithOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $file = new TemporaryFile(); + // ここでテンポラリファイルを使った処理を想定 + } + $this->__reportMemory('after ', $before); + } + +/** + * __reportMemory + * + * @param string $string + * @param int|null $before + * @return int + */ + private function __reportMemory(string $string, int $before = null) { + $current = memory_get_usage(false); + print $string . ':' . number_format($current); + if ($before !== null) { + print "\n"; + print $string . '(diff):' . number_format($current - $before); + } + print "\n"; + return $current; + } +} diff --git a/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php b/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php index 4a1d9e4..9e96734 100644 --- a/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php +++ b/Test/Case/Utility/TemporaryFile/TemporaryFileTest.php @@ -9,7 +9,6 @@ App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); App::uses('NetCommonsControllerTestCase', 'NetCommons.TestSuite'); App::uses('TemporaryFile', 'Files.Utility'); -App::uses('TemporaryFile2', 'Files.Utility'); /** * Summary for AttachmentBehavior Test Case @@ -48,101 +47,4 @@ public function testDelete() { $temporaryFile->delete(); $this->assertFalse(file_exists($path)); } - public function testMemoryLeakForKeepFiles() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $files[] = new TemporaryFile(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakForKeepFilesByTemporaryFile2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $files[] = new TemporaryFile2(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithDelete() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $file = new TemporaryFile(); - $files[] =$file; - $file->delete(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithDeleteByTemporaryFile2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $file = new TemporaryFile2(); - $files[] =$file; - $file->delete(); - } - $this->reportMemory('after ', $before); - } - public function testMemoryLeakWithDeleteAndOverwrite() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFile(); - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithDeleteAndOverwriteByTemporaryFile2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFile2(); - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithOverwrite() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $file = new TemporaryFile(); - // ここでテンポラリファイルを使った処理を想定 - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithOverwriteByTemporaryFile2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $file = new TemporaryFile2(); - // ここでテンポラリファイルを使った処理を想定 - } - $this->reportMemory('after ', $before); - } - - private function reportMemory(string $string, int $before = null) { - $current = memory_get_usage(false); - print($string . ':' . number_format($current)); - if ($before !== null) { - print "\n"; - print($string . '(diff):' . number_format($current - $before)); - } - print "\n"; - return $current; - } } diff --git a/Test/Case/Utility/TemporaryFolder/MemoryLeakTest.php b/Test/Case/Utility/TemporaryFolder/MemoryLeakTest.php new file mode 100644 index 0000000..a4a555b --- /dev/null +++ b/Test/Case/Utility/TemporaryFolder/MemoryLeakTest.php @@ -0,0 +1,105 @@ +markTestSkipped(); + } + +/** + * testMemoryLeakForKeepFolders + * メモリ使用量確認用コード。テスト対象外 + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakForKeepFolders() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $folders[] = new TemporaryFolder(); + } + $this->__reportMemory('after ', $before); + } + +/** + * testMemoryLeakWithDelete + * メモリ使用量確認用コード。テスト対象外 + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakWithDelete() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $folder = new TemporaryFolder(); + $folders[] = $folder; + $folder->delete(); + } + $this->__reportMemory('after ', $before); + } + +/** + * testMemoryLeakWithDeleteAndOverwrite + * メモリ使用量確認用コード。テスト対象外 + * + * @return void + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + */ + public function testMemoryLeakWithDeleteAndOverwrite() { + $max = 1000; + App::uses('TemporaryFolder', 'Files.Utility'); + $before = $this->__reportMemory('before'); + for ($i = 0; $i < $max; $i++) { + $folder = new TemporaryFolder(); + $folder->delete(); + } + $this->__reportMemory('after ', $before); + } + +/** + * __reportMemory + * + * @param string $string + * @param int|null $before + * @return int + */ + private function __reportMemory(string $string, int $before = null) { + $current = memory_get_usage(false); + print $string . ':' . number_format($current); + if ($before !== null) { + print "\n"; + print $string . '(diff):' . number_format($current - $before); + } + print "\n"; + return $current; + } +} diff --git a/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php b/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php index 9d4b007..9d796c0 100644 --- a/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php +++ b/Test/Case/Utility/TemporaryFolder/TemporaryFolderTest.php @@ -9,7 +9,6 @@ App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite'); App::uses('NetCommonsControllerTestCase', 'NetCommons.TestSuite'); App::uses('TemporaryFolder', 'Files.Utility'); -App::uses('TemporaryFolder2', 'Files.Utility'); /** * Summary for AttachmentBehavior Test Case @@ -48,78 +47,5 @@ public function testDelete() { $tempFolder->delete(); $this->assertFalse(file_exists($path)); } - public function testMemoryLeakForKeepFolders() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folders[] = new TemporaryFolder(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakForKeepFoldersByTemporaryFolder2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folders[] = new TemporaryFolder2(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithDelete() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFolder(); - $folders[] =$folder; - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - public function testMemoryLeakWithDeleteByTemporaryFolder2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFolder2(); - $folders[] =$folder; - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - public function testMemoryLeakWithDeleteAndOverwrite() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFolder(); - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - - public function testMemoryLeakWithDeleteAndOverwriteByTemporaryFolder2() { - $max = 1000; - App::uses('TemporaryFolder', 'Files.Utility'); - $before = $this->reportMemory('before'); - for ($i = 0; $i< $max; $i++) { - $folder = new TemporaryFolder2(); - $folder->delete(); - } - $this->reportMemory('after ', $before); - } - private function reportMemory(string $string, int $before = null) { - $current = memory_get_usage(false); - print($string . ':' . number_format($current)); - if ($before !== null) { - print "\n"; - print($string . '(diff):' . number_format($current - $before)); - } - print "\n"; - return $current; - } } diff --git a/Utility/TemporaryFile.php b/Utility/TemporaryFile.php index 155b0e3..3959cdf 100644 --- a/Utility/TemporaryFile.php +++ b/Utility/TemporaryFile.php @@ -9,6 +9,7 @@ App::uses('File', 'Utility'); App::uses('TemporaryFolder', 'Files.Utility'); +App::uses('TemporaryFolder2', 'Files.Utility'); App::uses('Security', 'Utility'); /** @@ -16,6 +17,16 @@ */ class TemporaryFile extends File { +/** + * @var array このクラスで作成されたテンポラリファイルのリスト + */ + private static $__filePaths = []; + +/** + * @var boola register_shutdown_functionに登録済みか + */ + private static $__isRegisteredShutdownFunction = false; + /** * @var TemporaryFolder テンポラリファイルを配置するテンポラリフォルダ */ @@ -33,8 +44,42 @@ public function __construct($folderPath = null) { } $fileName = Security::hash(mt_rand() . microtime(), 'md5'); - register_shutdown_function(array($this, 'delete')); + $path = $folderPath . DS . $fileName; + + self::$__filePaths[] = $path; + if (!self::$__isRegisteredShutdownFunction) { + register_shutdown_function([TemporaryFile::CLASS, 'deleteAll']); + self::$__isRegisteredShutdownFunction = true; + } + parent::__construct($path, true); + } + +/** + * 削除 + * + * @return void + */ + public function delete() { + $key = array_search($this->path, self::$__filePaths); + if ($key !== false) { + unset(self::$__filePaths[$key]); + if ($this->_tmpFolder instanceof TemporaryFolder) { + $this->_tmpFolder->delete(); + } + } + parent::delete(); + } - parent::__construct($folderPath . DS . $fileName, true); +/** + * 全テンポラリファイル削除 + * + * @return void + */ + public static function deleteAll() { + foreach (self::$__filePaths as $path) { + $file = new File($path); + $file->delete(); + } + self::$__filePaths = []; } } diff --git a/Utility/TemporaryFile2.php b/Utility/TemporaryFile2.php deleted file mode 100644 index 1a37b55..0000000 --- a/Utility/TemporaryFile2.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @link http://www.netcommons.org NetCommons Project - * @license http://www.netcommons.org/license.txt NetCommons License - */ - -App::uses('File', 'Utility'); -App::uses('TemporaryFolder', 'Files.Utility'); -App::uses('TemporaryFolder2', 'Files.Utility'); -App::uses('Security', 'Utility'); - -/** - * Class TemporaryFile - */ -class TemporaryFile2 extends File { - - private static $filePaths = []; - private static $isRegisteredShutdownFunction = false; - -/** - * @var TemporaryFolder テンポラリファイルを配置するテンポラリフォルダ - */ - protected $_tmpFolder; - -/** - * TemporaryFile constructor. - * - * @param string $folderPath テンポラリフォルダを作成するフォルダパス。指定されなければテンポラリフォルダを作成する - */ - public function __construct($folderPath = null) { - if ($folderPath === null) { - $this->_tmpFolder = new TemporaryFolder2(); - $folderPath = $this->_tmpFolder->path; - } - $fileName = Security::hash(mt_rand() . microtime(), 'md5'); - - $path = $folderPath . DS . $fileName; - - self::$filePaths[] = $path; - if (!self::$isRegisteredShutdownFunction) { - register_shutdown_function([TemporaryFile2::class, 'deleteAll']); - self::$isRegisteredShutdownFunction = true; - } - parent::__construct($path, true); - } - public function delete() { - $key = array_search($this->path, self::$filePaths); - if ($key !== false) { - unset(self::$filePaths[$key]); - if ($this->_tmpFolder instanceof TemporaryFolder2) { - $this->_tmpFolder->delete(); - } - } - parent::delete(); - } - - public static function deleteAll() { - foreach (self::$filePaths as $path) { - $file = new File($path); - $file->delete(); - } - self::$filePaths = []; - } -} diff --git a/Utility/TemporaryFolder.php b/Utility/TemporaryFolder.php index cb801d0..2f97862 100644 --- a/Utility/TemporaryFolder.php +++ b/Utility/TemporaryFolder.php @@ -24,6 +24,16 @@ */ class TemporaryFolder extends Folder { +/** + * @var string[] このクラスで作成されたテンポラリフォルダリスト + */ + private static $__folderPaths = []; + +/** + * @var bool register_shutdown_functionに登録済みか + */ + private static $__isRegisteredShutdownFunction = false; + /** * TemporaryFolder constructor. */ @@ -32,16 +42,41 @@ public function __construct() { $path .= Security::hash(mt_rand() . microtime(), 'md5'); //$mode = '0775'; // ε(     v ゚ω゚) <パーミッションいくつが適切だ? $mode = false; // とりあえずデフォルトのまま - register_shutdown_function(array($this, 'delete')); + self::$__folderPaths[] = $path; + if (!self::$__isRegisteredShutdownFunction) { + register_shutdown_function([TemporaryFolder::CLASS, 'deleteAll']); + self::$__isRegisteredShutdownFunction = true; + } parent::__construct($path, true, $mode); } /** - * デストラクタ + * 削除 + * + * @param string|null $path 削除対象パス + * @return bool + */ + public function delete($path = null) { + $path = $path ?? $this->path; + if ($path) { + $key = array_search($path, self::$__folderPaths); + if ($key !== false) { + unset(self::$__folderPaths[$key]); + } + } + return parent::delete($path); + } + +/** + * 全テンポラリフォルダの削除 * * @return void */ - //public function __destruct() { - // $this->delete(); - //} -} \ No newline at end of file + public static function deleteAll() { + $folder = new Folder(); + foreach (self::$__folderPaths as $path) { + $folder->delete($path); + } + self::$__folderPaths = []; + } +} diff --git a/Utility/TemporaryFolder2.php b/Utility/TemporaryFolder2.php deleted file mode 100644 index a9e7e48..0000000 --- a/Utility/TemporaryFolder2.php +++ /dev/null @@ -1,64 +0,0 @@ -path パス取得 - * // あれこれ - * $tempFolder->delete(); // 不要になったらdelete - * ``` - */ -class TemporaryFolder2 extends Folder { - - private static $folderPaths = []; - private static $isRegisteredShutdownFunction = false; - -/** - * TemporaryFolder constructor. - */ - public function __construct() { - $path = TMP; - $path .= Security::hash(mt_rand() . microtime(), 'md5'); - //$mode = '0775'; // ε(     v ゚ω゚) <パーミッションいくつが適切だ? - $mode = false; // とりあえずデフォルトのまま - self::$folderPaths[] = $path; - if (!self::$isRegisteredShutdownFunction) { - register_shutdown_function([TemporaryFolder2::class, 'deleteAll']); - self::$isRegisteredShutdownFunction = true; - } - parent::__construct($path, true, $mode); - } - - public function delete($path = null) { - $path = $path ?? $this->path; - if ($path) { - $key = array_search($path, self::$folderPaths); - if ($key !== false) { - unset(self::$folderPaths[$key]); - } - } - parent::delete($path); - } - - public static function deleteAll() { - $folder = new Folder(); - foreach (self::$folderPaths as $path) { - $folder->delete($path); - } - self::$folderPaths = []; - } -} \ No newline at end of file From 0d58adc8627126b3de29ced65e818ef41f20dfc7 Mon Sep 17 00:00:00 2001 From: Ryuji AMANO Date: Thu, 7 Mar 2024 15:33:21 +0900 Subject: [PATCH 15/16] =?UTF-8?q?change:=20=E4=B8=8D=E8=A6=81=E3=81=AAApp:?= =?UTF-8?q?:use=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utility/TemporaryFile.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Utility/TemporaryFile.php b/Utility/TemporaryFile.php index 3959cdf..20808b1 100644 --- a/Utility/TemporaryFile.php +++ b/Utility/TemporaryFile.php @@ -9,7 +9,6 @@ App::uses('File', 'Utility'); App::uses('TemporaryFolder', 'Files.Utility'); -App::uses('TemporaryFolder2', 'Files.Utility'); App::uses('Security', 'Utility'); /** From 9708d62b5c874746a14801d580b1174a680d150b Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Thu, 23 May 2024 18:03:32 +0900 Subject: [PATCH 16/16] =?UTF-8?q?change:=20wysiwyg=E3=81=AE=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AF=E3=82=AB=E3=82=A6=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=A2=E3=83=83=E3=83=97=E3=81=97=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/Component/DownloadComponent.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Controller/Component/DownloadComponent.php b/Controller/Component/DownloadComponent.php index fcb0c94..14e4d56 100644 --- a/Controller/Component/DownloadComponent.php +++ b/Controller/Component/DownloadComponent.php @@ -219,7 +219,10 @@ protected function _downloadUploadFile($file, $size, $options) { } // Download カウントアップ - $UploadFile->countUp($file); + $pluginKey = $file['UploadFile']['plugin_key'] ?? null; + if ($pluginKey !== 'wysiwyg') { + $UploadFile->countUp($file); + } return $this->_controller->response; }