From 755a08d84f30a75ad07b959323d347cf0f785f30 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFs=20Poujade?= <lois.poujade@tetras-libre.fr>
Date: Thu, 28 Apr 2022 15:30:40 +0200
Subject: [PATCH] Save edited image as base64 in metadata

---
 capsule-prototype/js/online-rekall/Tag.js |  4 +--
 capsule-prototype/js/online-script.js     | 35 ++++++++++++++++-------
 capsule-prototype/js/rekall/Document.js   |  7 +++--
 capsule-prototype/php/upload.php          | 11 +++----
 4 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/capsule-prototype/js/online-rekall/Tag.js b/capsule-prototype/js/online-rekall/Tag.js
index ba2cb34..f45f159 100644
--- a/capsule-prototype/js/online-rekall/Tag.js
+++ b/capsule-prototype/js/online-rekall/Tag.js
@@ -98,8 +98,8 @@ Tag.prototype.openQuickLook = function() {
 Tag.prototype.downloadFile = function() {
 	return this.document.downloadFile();
 }
-Tag.prototype.getDownloadLink = function() {
-  return this.document.getDownloadLink();
+Tag.prototype.getDownloadLink = function(original = false) {
+  return this.document.getDownloadLink(original);
 }
 Tag.prototype.openBrowser = function() {
 	return this.document.openBrowser();
diff --git a/capsule-prototype/js/online-script.js b/capsule-prototype/js/online-script.js
index 628fc45..7032a23 100644
--- a/capsule-prototype/js/online-script.js
+++ b/capsule-prototype/js/online-script.js
@@ -553,13 +553,29 @@ function setEditionControls() {
     var keyDoc = ev.target.parentElement.parentElement.attributes['keydoc'];
     var markerArea = new markerjs2.MarkerArea(document.getElementById('annotation_img_edit'));
     markerArea.targetRoot = document.getElementById('edit_pic_modal');
-    markerArea.addEventListener('render', () => {
+    markerArea.addEventListener('render', (event) => {
       var state = markerArea.getState();
       window.my_current_markerjs_data_in_ugly_global = state;
       markerArea.close(true);
       setMetaFromDom(keyDoc.value, "Rekall->MarkerjsState", btoa(JSON.stringify(state)));
+      setMetaFromDom(keyDoc.value, "Rekall->EditedImage", event.dataUrl);
+
+      /* this will create another annotation with edited image (as raw file, not b64 data)
+      var b64img = event.dataUrl;
+      var data = b64img.match(/data:([a-z]+)\/([a-z]+);base64,(.*)/);
+      var type = data[1], subtype = data[2], b64data = data[3];
+      var bin_str = atob(b64data);
+      var n = b64data.length;
+      var uar = new Uint8Array(n);
+      while (n--) uar[n] = bin_str.charCodeAt(n);
+      var file = new File([uar], 'edited_' + Math.floor(Math.random()*100) + '.' + subtype, {type: type+'/'+subtype});
+      $('#left_menu_item_btn_addfile').files = [file];
+      uploadFiles([file]);
+      */
+
     });
     markerArea.addEventListener('close', () => $('#edit_pic_modal').hide());
+    markerArea.renderAtNaturalSize = true;
     markerArea.show();
     if (window.my_current_markerjs_data_in_ugly_global)
       markerArea.restoreState(window.my_current_markerjs_data_in_ugly_global);
@@ -973,8 +989,8 @@ function fillPopupEdit(tag) {
 		$("#popupImg").attr("src","../shared/css/images/img-note.png");
 	} else {
 		if(tag.thumbnail.url){
-			$("#popupImg").attr("src",tag.thumbnail.url);
-      $('#annotation_img_edit').attr('src', tag.getDownloadLink());
+			$("#popupImg").attr("src", tag.getMetadata('Rekall->EditedImage') || tag.thumbnail.url);
+      $('#annotation_img_edit').attr('src', tag.getDownloadLink(true));
 		} else {
 			if(type.indexOf("image") > -1) $("#popupImg").attr("src","../shared/css/images/img-image.png")
 			else if(type.indexOf("pdf") > -1) $("#popupImg").attr("src","../shared/css/images/img-pdf.png")
@@ -999,16 +1015,13 @@ function fillPopupEdit(tag) {
 	$("#popupNom").css("color",tag.color);
 
   window.my_current_markerjs_data_in_ugly_global = null;
+  $('#editAnnotationPic').hide();
   if (type.split('/')[0] == 'image') {
-    $('#editAnnotationPic').html('edit');
+  $('#editAnnotationPic').html('edit');
+  $('#editAnnotationPic').show();
     var data = tag.getMetadata('Rekall->MarkerjsState');
-    if (data) {
-      var _data = JSON.parse(atob(data));
-      window.my_current_markerjs_data_in_ugly_global = _data;
-      // var _mjs = new mjslive.MarkerView(document.getElementById('popupImg'));
-      // _mjs.targetRoot = document.getElementById('popupImg').parentElement;
-      // _mjs.show(_data);
-    }
+    if (data)
+      window.my_current_markerjs_data_in_ugly_global = JSON.parse(atob(data));
   }
 
 	$("#popupType").html(type).css("color",tag.color);
diff --git a/capsule-prototype/js/rekall/Document.js b/capsule-prototype/js/rekall/Document.js
index 4ba477d..5627a3c 100644
--- a/capsule-prototype/js/rekall/Document.js
+++ b/capsule-prototype/js/rekall/Document.js
@@ -86,8 +86,11 @@ Document.prototype.isLink = function(version) {
 	else
 		return type.startsWith("rekall/link"); 
 }
-Document.prototype.getDownloadLink = function() {
-       return Utils.getLocalFilePath(this, "file");
+Document.prototype.getDownloadLink = function(original = false) {
+  var path = Utils.getLocalFilePath(this, "file");
+  if (original)
+    path = path.replace('file.php?r=', 'file.php?r=original_');
+  return path;
 }
 
 
diff --git a/capsule-prototype/php/upload.php b/capsule-prototype/php/upload.php
index c069ed2..36e882d 100644
--- a/capsule-prototype/php/upload.php
+++ b/capsule-prototype/php/upload.php
@@ -57,10 +57,11 @@
 			
 			if(is_uploaded_file($_FILES[$fileinfo]['tmp_name'])) {
 				if(file_exists($_FILES[$fileinfo]['tmp_name'])) {
-					if(!file_exists($uploadFolder.$filename)) {
-						if(is_writable($uploadFolder)) {
+          if(is_writable($uploadFolder)) {
+              if(!file_exists($uploadFolder.$filename)) {
 							//echo "Upload de ".$fileinfo."\t".$filename."...";
 							if(move_uploaded_file($_FILES[$fileinfo]['tmp_name'], $uploadFolder.$filename)) {
+                copy($uploadFolder.$filename, $uploadFolder.'original_'.$filename);
 								$metasAdded = addFileToProject($uploadFolder.$filename, $metas, $tcIn, $tcOut);
 								$key = $metasAdded["key"];
 								unset($metasAdded["key"]);
@@ -71,10 +72,10 @@
 								$retour .= '"code":0, "error":"Server error (copy of '.$uploadFolder.$filename.' is impossible)"';
 						}
 						else
-							$retour .= '"code":-1, "error":"The upload folder '.$uploadFolder.' has not enough permissions"';
+              $retour .= '"code":-2, "error":"File is already in project"';
 					}
 					else
-						$retour .= '"code":-2, "error":"File is already in project"';
+            $retour .= '"code":-1, "error":"The upload folder '.$uploadFolder.' has not enough permissions"';
 				} 
 				else
 					$retour .= '"code":-3, "error":"Server error in upload (temporary file missing)"';
@@ -154,4 +155,4 @@
 		    <input type="submit" value="Upload Image" name="submit">
 		</form>
 	</body>
-</html>
\ No newline at end of file
+</html>
-- 
GitLab