From 975675a68a77258efc7e15def9eaeee62757511c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFs=20Poujade?= <lois.poujade@tetras-libre.fr>
Date: Mon, 2 May 2022 16:56:49 +0200
Subject: [PATCH] Add command to keep original files aside from edited

+ fix reload of edited image in edition popup
---
 capsule-prototype/js/online-script.js         |   1 +
 capsule-prototype/js/rekall/Document.js       |   2 +-
 .../RekallProjectDuplicateImagesCommand.php   | 129 ++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 src/Command/RekallProjectDuplicateImagesCommand.php

diff --git a/capsule-prototype/js/online-script.js b/capsule-prototype/js/online-script.js
index c28f555..34d5238 100644
--- a/capsule-prototype/js/online-script.js
+++ b/capsule-prototype/js/online-script.js
@@ -569,6 +569,7 @@ function setEditionControls() {
       var file = new File([uar], keyDoc.value.substr(1), {type: type+'/'+subtype});
       $('#left_menu_item_btn_addfile').files = [file];
       uploadFiles([file], {'edited': 1});
+      $('#popupImg').attr('src', $('#popupImg').attr('src') + '&time=' + performance.now());
     });
     markerArea.addEventListener('close', () => $('#edit_pic_modal').hide());
     markerArea.renderAtNaturalSize = true;
diff --git a/capsule-prototype/js/rekall/Document.js b/capsule-prototype/js/rekall/Document.js
index 0582b29..f06c3ba 100644
--- a/capsule-prototype/js/rekall/Document.js
+++ b/capsule-prototype/js/rekall/Document.js
@@ -88,7 +88,7 @@ Document.prototype.isLink = function(version) {
 }
 Document.prototype.getDownloadLink = function(original = false) {
   var path = Utils.getLocalFilePath(this, "file");
-  return original ? path.replace('file.php?r=', 'file.php?r=original_') : path;
+  return original ? path.replace('file.php?r=', 'file.php?r=rk_original_af7ef02e_') : path;
 }
 Document.prototype.getFileName = function() {
   var path = Utils.getLocalFilePath(this, "file");
diff --git a/src/Command/RekallProjectDuplicateImagesCommand.php b/src/Command/RekallProjectDuplicateImagesCommand.php
new file mode 100644
index 0000000..990581d
--- /dev/null
+++ b/src/Command/RekallProjectDuplicateImagesCommand.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace App\Command;
+
+use LogicException;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\ProgressBar;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\ConsoleOutputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Filesystem\Filesystem;
+use Symfony\Component\Filesystem\Path;
+use Symfony\Component\HttpKernel\KernelInterface;
+
+#[AsCommand(
+    name: 'rekall:project:duplicate-images',
+    description: 'Duplicate each JPG/PNG/GIF file, creating for each a corresponding rk_original_af7ef02e_<filename>.xyz file',
+)]
+class RekallProjectDuplicateImagesCommand extends Command
+{
+    private Filesystem $file_system;
+    private KernelInterface $kernel;
+
+    private string $legacy_root;
+
+    private static string $LEGACY_DIRECTORY_NAME = 'legacy';
+    private static string $IMG_GLOB = '*.{[pP][nN][gG],[jJ][pP][gG],[jJ][pP][eE][gG],[gG][iI][fF]}';
+    private static string $ORIGINAL_FILE_PREPEND_WITH = 'rk_original_af7ef02e_';
+
+    public function __construct(Filesystem $filesystem, KernelInterface $kernel)
+    {
+        parent::__construct();
+        $this->file_system = $filesystem;
+        $this->kernel = $kernel;
+        $this->legacy_root = Path::join($this->kernel->getProjectDir(), self::$LEGACY_DIRECTORY_NAME);
+    }
+
+    protected function configure(): void { }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        if (!$output instanceof ConsoleOutputInterface) {
+            throw new LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
+        }
+
+        ProgressBar::setFormatDefinition(
+            'project_update_message',
+            ' %current%/%max% [%bar%] %percent%% : Updating project :%project% %elapsed%'
+        );
+        $output->writeln('Updating images for all projects started');
+
+        $all_file_in_directory = $this->getProjectsDirectories();
+        if (false === $all_file_in_directory) {
+            return Command::FAILURE;
+        }
+
+        $all_projects_progress_bar = new ProgressBar($output->section());
+        $all_projects_progress_bar->setFormat(
+            '%current%/%max% [%bar%] %percent%% : Overall project update progressing (%elapsed%)'
+        );
+        $all_projects_progress_bar->SetMessage('Projects update progress');
+        $all_projects_progress_bar->start();
+
+        foreach ($all_projects_progress_bar->iterate($all_file_in_directory) as $project_directory) {
+            $current_legacy_path = Path::join( $project_directory, 'file');
+
+            if (!$this->file_system->exists($current_legacy_path)) {
+                $output->writeln("'$current_legacy_path' don't exists");
+                continue;
+            }
+
+            if (!is_dir($current_legacy_path)) {
+                $output->writeln("'$current_legacy_path' is not a directory");
+                continue;
+            }
+
+            $this->updateProjectDirectory(
+                $output,
+                $current_legacy_path
+            );
+            $all_projects_progress_bar->advance();
+        }
+
+        $all_projects_progress_bar->finish();
+
+        return Command::SUCCESS;
+    }
+
+    /**
+     * @return string[]|false
+     */
+    private function getProjectsDirectories(): array|false
+    {
+        $all_directories_and_files = glob($this->legacy_root . '/*');
+        if (false === $all_directories_and_files) {
+            return false;
+        }
+        return $all_directories_and_files;
+    }
+
+    /**
+     * @param ConsoleOutputInterface $output
+     * @param string $current_legacy_path
+     * @return void
+     */
+    protected function updateProjectDirectory(
+        ConsoleOutputInterface $output,
+        string $current_legacy_path
+    ): void {
+        $output->writeln("Updating project :$current_legacy_path");
+        $success = $failed = 0;
+
+        foreach (glob("$current_legacy_path/".self::$IMG_GLOB, GLOB_BRACE) as $filename) {
+          if (str_starts_with($filename, $current_legacy_path.'/'.self::$ORIGINAL_FILE_PREPEND_WITH))
+            continue;
+          $target = str_replace($current_legacy_path.'/', $current_legacy_path.'/'.self::$ORIGINAL_FILE_PREPEND_WITH, $filename);
+          if (!file_exists($target)) {
+            if (!copy($filename, $target)) {
+              $output->writeln("Failed to copy '$filename' to '$target'");
+              $failed++;
+            } else
+              $success++;
+          }
+        }
+
+        $output->writeln("Updated project :$current_legacy_path, $success files duplicated, $failed failed ---> DONE");
+    }
+}
-- 
GitLab