jclyo1 commited on
Commit
6b6586d
1 Parent(s): 5ba48ce
Files changed (2) hide show
  1. static/index.html +1 -472
  2. static/script.js +458 -0
static/index.html CHANGED
@@ -28,6 +28,7 @@
28
  <link rel="stylesheet" href="json_viewer.css" />
29
  <script src="https://unpkg.com/@peculiar/x509"></script>
30
  <script type="text/javascript" src="json_viewer.js"></script>
 
31
  </head>
32
  <body>
33
  <div class="container-fluid mt-2" id="head">
@@ -393,476 +394,4 @@
393
  </div>
394
  </body>
395
 
396
- <script>
397
- const generateTab = document.querySelector("#generateTab");
398
- const verifyTab = document.querySelector("#verifyTab");
399
- const displayVerify = document.querySelectorAll(".display-verify");
400
- const displayGenerate = document.querySelectorAll(".display-generate");
401
- const uploadForm = document.querySelector(".verify-upload-form");
402
- const imageGenForm = document.querySelector(".image-gen-form");
403
- const imagePrompt = document.getElementById("prompt");
404
- const model = document.getElementById("model");
405
- const generateTerms = document.getElementById("generate-terms");
406
- const generateButton = document.getElementById("generate-button");
407
-
408
- const fileUpload = document.getElementById("fileUpload");
409
- const verifyButton = document.getElementById("verify-button");
410
- const verifyTerms = document.getElementById("verify-terms");
411
-
412
- const generateImageContainer = document.querySelector(
413
- ".display-generate .image-container"
414
- );
415
- const generateActionMenu = document.querySelector(".action-menu");
416
- const verifyImageContainer = document.querySelector(
417
- ".display-verify .image-container"
418
- );
419
- const uploadedImageContainer = document.querySelector("#original-image");
420
- const downloadButton = document.getElementById("download-button");
421
- const verifyResultDescription = document.getElementById(
422
- "verifyResultDescription"
423
- );
424
- const verificationOutput = document.getElementById("verification-output");
425
- const certificateList = document.getElementById("certificate-list");
426
- const outputContainer = document.querySelector(".output-container");
427
- const contentCredentialsIcon = document.getElementById(
428
- "content-crendentials-icon"
429
- );
430
- const digitalWatermarkIcon = document.getElementById(
431
- "digital-watermark-icon"
432
- );
433
-
434
- var certificates = [];
435
-
436
- [imagePrompt, model, generateTerms].forEach((item) => {
437
- item.addEventListener("change", async (event) => {
438
- setGenerateButtonStatus();
439
- });
440
- });
441
-
442
- const setGenerateButtonStatus = () => {
443
- if (imagePrompt.value && model.value && generateTerms.checked)
444
- generateButton.classList.add("active");
445
- else generateButton.classList.remove("active");
446
- };
447
-
448
- [fileUpload, verifyTerms].forEach((item) => {
449
- item.addEventListener("change", async (event) => {
450
- setVerifyButtonStatus();
451
- });
452
- });
453
-
454
- const setVerifyButtonStatus = () => {
455
- if (fileUpload.value && verifyTerms.checked)
456
- verifyButton.classList.add("active");
457
- else verifyButton.classList.remove("active");
458
- };
459
-
460
- generateTab.addEventListener("click", (event) => {
461
- event.target.classList.add("active");
462
- verifyTab.classList.remove("active");
463
-
464
- setGenerateElementsDisplay("block");
465
- setVerifyElementsDisplay("none");
466
- });
467
-
468
- verifyTab.addEventListener("click", (event) => {
469
- event.target.classList.add("active");
470
- generateTab.classList.remove("active");
471
-
472
- setGenerateElementsDisplay("none");
473
- setVerifyElementsDisplay("block");
474
- });
475
-
476
- document
477
- .getElementById("goto-verify-button")
478
- .addEventListener("click", (event) => {
479
- verifyTab.classList.add("active");
480
- generateTab.classList.remove("active");
481
-
482
- setVerifyElementsDisplay("block");
483
- setGenerateElementsDisplay("none");
484
- });
485
-
486
- function setGenerateElementsDisplay(displayStatus) {
487
- displayGenerate.forEach((item) => {
488
- item.style.display = displayStatus;
489
- });
490
- }
491
-
492
- function setVerifyElementsDisplay(displayStatus) {
493
- displayVerify.forEach((item) => {
494
- item.style.display = displayStatus;
495
- });
496
- }
497
-
498
- uploadForm.addEventListener("submit", (e) => {
499
- e.preventDefault();
500
- submitForm();
501
- });
502
-
503
- function submitForm() {
504
- if (!fileUpload.value || !verifyTerms.checked) return;
505
-
506
- const file = document.getElementById("fileUpload").files[0];
507
-
508
- let fileReader = new FileReader();
509
- fileReader.readAsDataURL(file);
510
- fileReader.onload = function () {
511
- document
512
- .getElementById("uploaded-image")
513
- .setAttribute("src", fileReader.result);
514
- };
515
-
516
- document.querySelector(".display-verify .error").style.display = "none";
517
- placeholder = document.querySelector(".display-verify .placeholder");
518
- spinner = document.querySelector(".display-verify .spinner");
519
- placeholder.style.display = "none";
520
-
521
- if (document.getElementById("verifyresult"))
522
- document.getElementById("verifyresult").remove(); // JCL make sure to remove the correct one
523
- spinner.style.display = "block";
524
-
525
- outputContainer.style.display = "none";
526
- document.getElementById("resultLabel").style.display = "none";
527
- uploadedImageContainer.style.display = "none";
528
-
529
- const formData = new FormData(uploadForm);
530
-
531
- // Add additional form data as needed
532
- //formData.append('additionalData', 'additionalValue');
533
-
534
- // Call function to submit form data
535
- submitFormData(formData);
536
- }
537
-
538
- function submitFormData(formData) {
539
- fetch("verify", {
540
- method: "POST",
541
- body: formData,
542
- })
543
- .then((response) => response.json())
544
- .then((data) => {
545
- console.log(data);
546
-
547
- fileUpload.value = "";
548
- setVerifyButtonStatus();
549
-
550
- if (data.contains_c2pa == "true") {
551
- contentCredentialsIcon.setAttribute("class", "check-icon");
552
- } else {
553
- contentCredentialsIcon.setAttribute("class", "x-icon");
554
- }
555
-
556
- if (data.contains_watermark == "true") {
557
- digitalWatermarkIcon.setAttribute("class", "check-icon");
558
- } else {
559
- digitalWatermarkIcon.setAttribute("class", "x-icon");
560
- }
561
-
562
- if (
563
- data.contains_c2pa == "true" &&
564
- data.contains_watermark == "true"
565
- ) {
566
- verifyResultDescription.innerHTML =
567
- "Your image contains content credentials, which are displayed below.";
568
- } else if (
569
- data.contains_c2pa == "false" &&
570
- data.contains_watermark == "true"
571
- ) {
572
- verifyResultDescription.innerHTML =
573
- "The watermark was found, but image modifications were also detected. The last untampered, signed version on file is displayed.";
574
- } else if (
575
- data.contains_c2pa == "true" &&
576
- data.contains_watermark == "false"
577
- ) {
578
- verifyResultDescription.innerHTML =
579
- "Your image contains content credentials, which are displayed below. A watermark was not detected.";
580
- } else if (
581
- data.contains_c2pa == "false" &&
582
- data.contains_watermark == "false"
583
- ) {
584
- verifyResultDescription.innerHTML =
585
- "Nothing to show: this image contains neither content credentials, nor a watermark.";
586
- }
587
-
588
- uploadedImageContainer.style.display = "flex";
589
- document.getElementById("resultLabel").style.display = "block";
590
-
591
- document.querySelector(".display-verify .spinner").style.display =
592
- "none";
593
-
594
- if (data.result_media != "n/a") {
595
- const path = "/" + data.result_media;
596
-
597
- var truepicDisplay = document.createElement("truepic-display");
598
-
599
- truepicDisplay.addEventListener(
600
- "validate",
601
- setVerificationOutputFromValidation
602
- );
603
- truepicDisplay.addEventListener(
604
- "validate",
605
- setCertificateOutputFromValidation
606
- );
607
-
608
- truepicDisplay.setAttribute("id", "verifyresult");
609
- truepicDisplay.setAttribute("active", "");
610
- var truepic = document.createElement("img");
611
- truepic.src = path;
612
-
613
- truepicDisplay.appendChild(truepic);
614
-
615
- verifyImageContainer.appendChild(truepicDisplay);
616
-
617
- outputContainer.style.display = "block";
618
- }
619
- // Handle response data
620
- })
621
- .catch((error) => {
622
- console.log(error);
623
- document.querySelector(".display-verify .error").style.display =
624
- "block";
625
-
626
- fileUpload.value = "";
627
- setVerifyButtonStatus();
628
-
629
- document.querySelector(".display-verify .spinner").style.display =
630
- "none";
631
- document.querySelector(".display-verify .placeholder").style.display =
632
- "block";
633
- // Handle errors
634
- });
635
- }
636
-
637
- const generateImage = async (text, model) => {
638
- const inferResponse = await fetch(
639
- `generate?prompt=${text}&model=${model}`
640
- );
641
- const inferJson = await inferResponse.json();
642
-
643
- return inferJson.response;
644
- };
645
-
646
- imageGenForm.addEventListener("submit", async (event) => {
647
- event.preventDefault();
648
-
649
- if (!imagePrompt.value || !model.value || !generateTerms.checked) return;
650
-
651
- placeholder = document.querySelector(".display-generate .placeholder");
652
- spinner = document.querySelector(".display-generate .spinner");
653
- generateActionMenu.style.display = "none";
654
- document.querySelector(".display-generate .error").style.display = "none";
655
-
656
- // parameters.style.display = "none";
657
-
658
- try {
659
- placeholder.style.display = "none";
660
- if (document.getElementById("genresult"))
661
- document.getElementById("genresult").remove(); // JCL make sure to remove the correct one
662
-
663
- spinner.style.display = "block";
664
-
665
- const resp = await generateImage(imagePrompt.value, model.value);
666
- const path = "/" + resp;
667
-
668
- var truepicDisplay = document.createElement("truepic-display");
669
-
670
- truepicDisplay.setAttribute("id", "genresult");
671
- truepicDisplay.setAttribute("active", "");
672
- var truepic = document.createElement("img");
673
- truepic.src = path;
674
-
675
- truepicDisplay.appendChild(truepic);
676
-
677
- spinner.style.display = "none";
678
- generateImageContainer.appendChild(truepicDisplay);
679
-
680
- downloadButton.href = path;
681
- downloadButton.download = resp;
682
-
683
- generateActionMenu.style.display = "block";
684
-
685
- /*
686
-
687
-
688
- modelParam.innerHTML = model.value;
689
- promptParam.innerHTML = textGenInput.value;
690
- parameters.style.display = "block";
691
- */
692
- } catch (err) {
693
- console.error(err);
694
- document.querySelector(".display-generate .error").style.display =
695
- "block";
696
- spinner.style.display = "none";
697
- placeholder.style.display = "block";
698
- }
699
- });
700
-
701
- function setVerificationOutputFromValidation(event) {
702
- //verificationDetails.style.display = "block";
703
- return setVerificationOutput(event.detail.manifestStore.toJSON());
704
- }
705
-
706
- function setCertificateOutputFromValidation(event) {
707
- return setCertificateOutput(event.detail.manifestStore);
708
- }
709
-
710
- function setVerificationOutput(output = null) {
711
- verificationOutput.innerHTML = "";
712
-
713
- if (!output) {
714
- return;
715
- }
716
-
717
- const viewer = new JSONViewer();
718
-
719
- verificationOutput.appendChild(viewer.getContainer());
720
-
721
- viewer.showJSON(output);
722
- }
723
-
724
- function setCertificateOutput(manifestStore = null) {
725
- const certificate = manifestStore?.activeManifest?.certificate;
726
-
727
- if (!certificate) {
728
- return;
729
- }
730
-
731
- certificates = [
732
- {
733
- der: certificate.der,
734
- name: certificate.subjectName,
735
- decoded: new x509.X509Certificate(certificate.der),
736
- },
737
- ...certificate.chain.map((certificate) => ({
738
- der: certificate.der,
739
- decoded: new x509.X509Certificate(certificate.der),
740
- })),
741
- ];
742
-
743
- certificates.forEach((certificate) => {
744
- certificate.transformed = transformCert(certificate.decoded);
745
- });
746
-
747
- console.log("certificates", certificates);
748
-
749
- certificateList.innerHTML = "";
750
-
751
- certificates.forEach((certificate, index) => {
752
- var li = document.createElement("li");
753
- if (index == 0) li.classList.add("active");
754
- li.appendChild(
755
- document.createTextNode(certificate.transformed.subjectCommonName)
756
- );
757
- li.addEventListener("click", function (e) {
758
- setCertificate(index);
759
- const lis = document.querySelectorAll("#certificate-list li");
760
-
761
- lis.forEach((element) => {
762
- element.classList.remove("active");
763
- });
764
-
765
- this.classList.add("active");
766
- });
767
-
768
- certificateList.appendChild(li);
769
- });
770
-
771
- setCertificate(0);
772
- }
773
-
774
- function transformCert(certificate) {
775
- const {
776
- issuer,
777
- subject,
778
- notAfter: expired,
779
- notBefore: issued,
780
- serialNumber,
781
- publicKey: {
782
- algorithm: {
783
- name: algorithm,
784
- modulusLength: modulus,
785
- namedCurve: namedCurve,
786
- },
787
- },
788
- } = certificate;
789
-
790
- const parsedSubject = parseCertificateValues(subject);
791
- const parsedIssuer = parseCertificateValues(issuer);
792
-
793
- return {
794
- issuerCommonName: parsedIssuer["CN"],
795
- issuerOrganizationUnit: parsedIssuer["OU"],
796
- issuerOrganization: parsedIssuer["O"],
797
- issuerCountry: parsedIssuer["C"],
798
- subjectCommonName: parsedSubject["CN"],
799
- subjectOrganizationUnit: parsedSubject["OU"],
800
- subjectOrganization: parsedSubject["O"],
801
- subjectCountry: parsedSubject["C"],
802
- issued,
803
- expired,
804
- serialNumber,
805
- algorithm,
806
- modulus,
807
- namedCurve,
808
- };
809
- }
810
-
811
- function parseCertificateValues(input) {
812
- const params = new URLSearchParams(input.replaceAll(",", "&"));
813
- const responses = {};
814
-
815
- for (const entry of params.entries()) {
816
- responses[entry[0].trim()] = entry[1];
817
- }
818
-
819
- return responses;
820
- }
821
-
822
- function setCertificate(ind) {
823
- const certificate = certificates[ind].transformed;
824
-
825
- document.querySelector(".details .issuerCommonName").innerHTML =
826
- certificate.issuerCommonName;
827
- document.querySelector(".details .issuerOrganizationUnit").innerHTML =
828
- certificate.issuerOrganizationUnit;
829
- document.querySelector(".details .issuerOrganization").innerHTML =
830
- certificate.issuerOrganization;
831
- document.querySelector(".details .issuerCountry").innerHTML =
832
- certificate.issuerCountry;
833
- document.querySelector(".details .subjectCommonName").innerHTML =
834
- certificate.subjectCommonName;
835
- document.querySelector(".details .subjectOrganizationUnit").innerHTML =
836
- certificate.subjectOrganizationUnit;
837
- document.querySelector(".details .subjectOrganization").innerHTML =
838
- certificate.subjectOrganization;
839
- document.querySelector(".details .subjectCountry").innerHTML =
840
- certificate.subjectCountry;
841
- document.querySelector(".details .issued").innerHTML = certificate.issued;
842
- document.querySelector(".details .expired").innerHTML =
843
- certificate.expired;
844
- document.querySelector(".details .serialNumber").innerHTML =
845
- certificate.serialNumber;
846
- document.querySelector(".details .algorithm").innerHTML =
847
- certificate.algorithm;
848
-
849
- /*
850
- if (certificate.modulus !== undefined) {
851
- document.querySelector(".details .modulus").innerHTML =
852
- certificate.modulus;
853
- document.querySelector("#modulusContainers").style.display = "block";
854
- } else {
855
- document.querySelector("#modulusContainers").style.display = "none";
856
- }
857
- */
858
-
859
- if (certificate.namedCurve !== undefined) {
860
- document.querySelector(".details .namedCurve").innerHTML =
861
- certificate.namedCurve;
862
- document.querySelector("#curveContainer").style.display = "block";
863
- } else {
864
- document.querySelector("#curveContainer").style.display = "none";
865
- }
866
- }
867
- </script>
868
  </html>
 
28
  <link rel="stylesheet" href="json_viewer.css" />
29
  <script src="https://unpkg.com/@peculiar/x509"></script>
30
  <script type="text/javascript" src="json_viewer.js"></script>
31
+ <script type="text/javascript" src="script.js"></script>
32
  </head>
33
  <body>
34
  <div class="container-fluid mt-2" id="head">
 
394
  </div>
395
  </body>
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
  </html>
static/script.js ADDED
@@ -0,0 +1,458 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const generateTab = document.querySelector("#generateTab");
2
+ const verifyTab = document.querySelector("#verifyTab");
3
+ const displayVerify = document.querySelectorAll(".display-verify");
4
+ const displayGenerate = document.querySelectorAll(".display-generate");
5
+ const uploadForm = document.querySelector(".verify-upload-form");
6
+ const imageGenForm = document.querySelector(".image-gen-form");
7
+ const imagePrompt = document.getElementById("prompt");
8
+ const model = document.getElementById("model");
9
+ const generateTerms = document.getElementById("generate-terms");
10
+ const generateButton = document.getElementById("generate-button");
11
+
12
+ const fileUpload = document.getElementById("fileUpload");
13
+ const verifyButton = document.getElementById("verify-button");
14
+ const verifyTerms = document.getElementById("verify-terms");
15
+
16
+ const generateImageContainer = document.querySelector(
17
+ ".display-generate .image-container"
18
+ );
19
+ const generateActionMenu = document.querySelector(".action-menu");
20
+ const verifyImageContainer = document.querySelector(
21
+ ".display-verify .image-container"
22
+ );
23
+ const uploadedImageContainer = document.querySelector("#original-image");
24
+ const downloadButton = document.getElementById("download-button");
25
+ const verifyResultDescription = document.getElementById(
26
+ "verifyResultDescription"
27
+ );
28
+ const verificationOutput = document.getElementById("verification-output");
29
+ const certificateList = document.getElementById("certificate-list");
30
+ const outputContainer = document.querySelector(".output-container");
31
+ const contentCredentialsIcon = document.getElementById(
32
+ "content-crendentials-icon"
33
+ );
34
+ const digitalWatermarkIcon = document.getElementById("digital-watermark-icon");
35
+
36
+ var certificates = [];
37
+
38
+ [imagePrompt, model, generateTerms].forEach((item) => {
39
+ item.addEventListener("change", async (event) => {
40
+ setGenerateButtonStatus();
41
+ });
42
+ });
43
+
44
+ const setGenerateButtonStatus = () => {
45
+ if (imagePrompt.value && model.value && generateTerms.checked)
46
+ generateButton.classList.add("active");
47
+ else generateButton.classList.remove("active");
48
+ };
49
+
50
+ [fileUpload, verifyTerms].forEach((item) => {
51
+ item.addEventListener("change", async (event) => {
52
+ setVerifyButtonStatus();
53
+ });
54
+ });
55
+
56
+ const setVerifyButtonStatus = () => {
57
+ if (fileUpload.value && verifyTerms.checked)
58
+ verifyButton.classList.add("active");
59
+ else verifyButton.classList.remove("active");
60
+ };
61
+
62
+ generateTab.addEventListener("click", (event) => {
63
+ event.target.classList.add("active");
64
+ verifyTab.classList.remove("active");
65
+
66
+ setGenerateElementsDisplay("block");
67
+ setVerifyElementsDisplay("none");
68
+ });
69
+
70
+ verifyTab.addEventListener("click", (event) => {
71
+ event.target.classList.add("active");
72
+ generateTab.classList.remove("active");
73
+
74
+ setGenerateElementsDisplay("none");
75
+ setVerifyElementsDisplay("block");
76
+ });
77
+
78
+ document
79
+ .getElementById("goto-verify-button")
80
+ .addEventListener("click", (event) => {
81
+ verifyTab.classList.add("active");
82
+ generateTab.classList.remove("active");
83
+
84
+ setVerifyElementsDisplay("block");
85
+ setGenerateElementsDisplay("none");
86
+ });
87
+
88
+ function setGenerateElementsDisplay(displayStatus) {
89
+ displayGenerate.forEach((item) => {
90
+ item.style.display = displayStatus;
91
+ });
92
+ }
93
+
94
+ function setVerifyElementsDisplay(displayStatus) {
95
+ displayVerify.forEach((item) => {
96
+ item.style.display = displayStatus;
97
+ });
98
+ }
99
+
100
+ uploadForm.addEventListener("submit", (e) => {
101
+ e.preventDefault();
102
+ submitForm();
103
+ });
104
+
105
+ function submitForm() {
106
+ if (!fileUpload.value || !verifyTerms.checked) return;
107
+
108
+ const file = document.getElementById("fileUpload").files[0];
109
+
110
+ let fileReader = new FileReader();
111
+ fileReader.readAsDataURL(file);
112
+ fileReader.onload = function () {
113
+ document
114
+ .getElementById("uploaded-image")
115
+ .setAttribute("src", fileReader.result);
116
+ };
117
+
118
+ document.querySelector(".display-verify .error").style.display = "none";
119
+ placeholder = document.querySelector(".display-verify .placeholder");
120
+ spinner = document.querySelector(".display-verify .spinner");
121
+ placeholder.style.display = "none";
122
+
123
+ if (document.getElementById("verifyresult"))
124
+ document.getElementById("verifyresult").remove(); // JCL make sure to remove the correct one
125
+ spinner.style.display = "block";
126
+
127
+ outputContainer.style.display = "none";
128
+ document.getElementById("resultLabel").style.display = "none";
129
+ uploadedImageContainer.style.display = "none";
130
+
131
+ const formData = new FormData(uploadForm);
132
+
133
+ // Add additional form data as needed
134
+ //formData.append('additionalData', 'additionalValue');
135
+
136
+ // Call function to submit form data
137
+ submitFormData(formData);
138
+ }
139
+
140
+ function submitFormData(formData) {
141
+ fetch("verify", {
142
+ method: "POST",
143
+ body: formData,
144
+ })
145
+ .then((response) => response.json())
146
+ .then((data) => {
147
+ console.log(data);
148
+
149
+ fileUpload.value = "";
150
+ setVerifyButtonStatus();
151
+
152
+ if (data.contains_c2pa == "true") {
153
+ contentCredentialsIcon.setAttribute("class", "check-icon");
154
+ } else {
155
+ contentCredentialsIcon.setAttribute("class", "x-icon");
156
+ }
157
+
158
+ if (data.contains_watermark == "true") {
159
+ digitalWatermarkIcon.setAttribute("class", "check-icon");
160
+ } else {
161
+ digitalWatermarkIcon.setAttribute("class", "x-icon");
162
+ }
163
+
164
+ if (data.contains_c2pa == "true" && data.contains_watermark == "true") {
165
+ verifyResultDescription.innerHTML =
166
+ "Your image contains content credentials, which are displayed below.";
167
+ } else if (
168
+ data.contains_c2pa == "false" &&
169
+ data.contains_watermark == "true"
170
+ ) {
171
+ verifyResultDescription.innerHTML =
172
+ "The watermark was found, but image modifications were also detected. The last untampered, signed version on file is displayed.";
173
+ } else if (
174
+ data.contains_c2pa == "true" &&
175
+ data.contains_watermark == "false"
176
+ ) {
177
+ verifyResultDescription.innerHTML =
178
+ "Your image contains content credentials, which are displayed below. A watermark was not detected.";
179
+ } else if (
180
+ data.contains_c2pa == "false" &&
181
+ data.contains_watermark == "false"
182
+ ) {
183
+ verifyResultDescription.innerHTML =
184
+ "Nothing to show: this image contains neither content credentials, nor a watermark.";
185
+ }
186
+
187
+ uploadedImageContainer.style.display = "flex";
188
+ document.getElementById("resultLabel").style.display = "block";
189
+
190
+ document.querySelector(".display-verify .spinner").style.display = "none";
191
+
192
+ if (data.result_media != "n/a") {
193
+ const path = "/" + data.result_media;
194
+
195
+ var truepicDisplay = document.createElement("truepic-display");
196
+
197
+ truepicDisplay.addEventListener(
198
+ "validate",
199
+ setVerificationOutputFromValidation
200
+ );
201
+ truepicDisplay.addEventListener(
202
+ "validate",
203
+ setCertificateOutputFromValidation
204
+ );
205
+
206
+ truepicDisplay.setAttribute("id", "verifyresult");
207
+ truepicDisplay.setAttribute("active", "");
208
+ var truepic = document.createElement("img");
209
+ truepic.src = path;
210
+
211
+ truepicDisplay.appendChild(truepic);
212
+
213
+ verifyImageContainer.appendChild(truepicDisplay);
214
+
215
+ outputContainer.style.display = "block";
216
+ }
217
+ // Handle response data
218
+ })
219
+ .catch((error) => {
220
+ console.log(error);
221
+ document.querySelector(".display-verify .error").style.display = "block";
222
+
223
+ fileUpload.value = "";
224
+ setVerifyButtonStatus();
225
+
226
+ document.querySelector(".display-verify .spinner").style.display = "none";
227
+ document.querySelector(".display-verify .placeholder").style.display =
228
+ "block";
229
+ // Handle errors
230
+ });
231
+ }
232
+
233
+ const generateImage = async (text, model) => {
234
+ const inferResponse = await fetch(`generate?prompt=${text}&model=${model}`);
235
+ const inferJson = await inferResponse.json();
236
+
237
+ return inferJson.response;
238
+ };
239
+
240
+ imageGenForm.addEventListener("submit", async (event) => {
241
+ event.preventDefault();
242
+
243
+ if (!imagePrompt.value || !model.value || !generateTerms.checked) return;
244
+
245
+ placeholder = document.querySelector(".display-generate .placeholder");
246
+ spinner = document.querySelector(".display-generate .spinner");
247
+ generateActionMenu.style.display = "none";
248
+ document.querySelector(".display-generate .error").style.display = "none";
249
+
250
+ // parameters.style.display = "none";
251
+
252
+ try {
253
+ placeholder.style.display = "none";
254
+ if (document.getElementById("genresult"))
255
+ document.getElementById("genresult").remove(); // JCL make sure to remove the correct one
256
+
257
+ spinner.style.display = "block";
258
+
259
+ const resp = await generateImage(imagePrompt.value, model.value);
260
+ const path = "/" + resp;
261
+
262
+ var truepicDisplay = document.createElement("truepic-display");
263
+
264
+ truepicDisplay.setAttribute("id", "genresult");
265
+ truepicDisplay.setAttribute("active", "");
266
+ var truepic = document.createElement("img");
267
+ truepic.src = path;
268
+
269
+ truepicDisplay.appendChild(truepic);
270
+
271
+ spinner.style.display = "none";
272
+ generateImageContainer.appendChild(truepicDisplay);
273
+
274
+ downloadButton.href = path;
275
+ downloadButton.download = resp;
276
+
277
+ generateActionMenu.style.display = "block";
278
+
279
+ /*
280
+
281
+
282
+ modelParam.innerHTML = model.value;
283
+ promptParam.innerHTML = textGenInput.value;
284
+ parameters.style.display = "block";
285
+ */
286
+ } catch (err) {
287
+ console.error(err);
288
+ document.querySelector(".display-generate .error").style.display = "block";
289
+ spinner.style.display = "none";
290
+ placeholder.style.display = "block";
291
+ }
292
+ });
293
+
294
+ function setVerificationOutputFromValidation(event) {
295
+ //verificationDetails.style.display = "block";
296
+ return setVerificationOutput(event.detail.manifestStore.toJSON());
297
+ }
298
+
299
+ function setCertificateOutputFromValidation(event) {
300
+ return setCertificateOutput(event.detail.manifestStore);
301
+ }
302
+
303
+ function setVerificationOutput(output = null) {
304
+ verificationOutput.innerHTML = "";
305
+
306
+ if (!output) {
307
+ return;
308
+ }
309
+
310
+ const viewer = new JSONViewer();
311
+
312
+ verificationOutput.appendChild(viewer.getContainer());
313
+
314
+ viewer.showJSON(output);
315
+ }
316
+
317
+ function setCertificateOutput(manifestStore = null) {
318
+ const certificate = manifestStore?.activeManifest?.certificate;
319
+
320
+ if (!certificate) {
321
+ return;
322
+ }
323
+
324
+ certificates = [
325
+ {
326
+ der: certificate.der,
327
+ name: certificate.subjectName,
328
+ decoded: new x509.X509Certificate(certificate.der),
329
+ },
330
+ ...certificate.chain.map((certificate) => ({
331
+ der: certificate.der,
332
+ decoded: new x509.X509Certificate(certificate.der),
333
+ })),
334
+ ];
335
+
336
+ certificates.forEach((certificate) => {
337
+ certificate.transformed = transformCert(certificate.decoded);
338
+ });
339
+
340
+ console.log("certificates", certificates);
341
+
342
+ certificateList.innerHTML = "";
343
+
344
+ certificates.forEach((certificate, index) => {
345
+ var li = document.createElement("li");
346
+ if (index == 0) li.classList.add("active");
347
+ li.appendChild(
348
+ document.createTextNode(certificate.transformed.subjectCommonName)
349
+ );
350
+ li.addEventListener("click", function (e) {
351
+ setCertificate(index);
352
+ const lis = document.querySelectorAll("#certificate-list li");
353
+
354
+ lis.forEach((element) => {
355
+ element.classList.remove("active");
356
+ });
357
+
358
+ this.classList.add("active");
359
+ });
360
+
361
+ certificateList.appendChild(li);
362
+ });
363
+
364
+ setCertificate(0);
365
+ }
366
+
367
+ function transformCert(certificate) {
368
+ const {
369
+ issuer,
370
+ subject,
371
+ notAfter: expired,
372
+ notBefore: issued,
373
+ serialNumber,
374
+ publicKey: {
375
+ algorithm: {
376
+ name: algorithm,
377
+ modulusLength: modulus,
378
+ namedCurve: namedCurve,
379
+ },
380
+ },
381
+ } = certificate;
382
+
383
+ const parsedSubject = parseCertificateValues(subject);
384
+ const parsedIssuer = parseCertificateValues(issuer);
385
+
386
+ return {
387
+ issuerCommonName: parsedIssuer["CN"],
388
+ issuerOrganizationUnit: parsedIssuer["OU"],
389
+ issuerOrganization: parsedIssuer["O"],
390
+ issuerCountry: parsedIssuer["C"],
391
+ subjectCommonName: parsedSubject["CN"],
392
+ subjectOrganizationUnit: parsedSubject["OU"],
393
+ subjectOrganization: parsedSubject["O"],
394
+ subjectCountry: parsedSubject["C"],
395
+ issued,
396
+ expired,
397
+ serialNumber,
398
+ algorithm,
399
+ modulus,
400
+ namedCurve,
401
+ };
402
+ }
403
+
404
+ function parseCertificateValues(input) {
405
+ const params = new URLSearchParams(input.replaceAll(",", "&"));
406
+ const responses = {};
407
+
408
+ for (const entry of params.entries()) {
409
+ responses[entry[0].trim()] = entry[1];
410
+ }
411
+
412
+ return responses;
413
+ }
414
+
415
+ function setCertificate(ind) {
416
+ const certificate = certificates[ind].transformed;
417
+
418
+ document.querySelector(".details .issuerCommonName").innerHTML =
419
+ certificate.issuerCommonName;
420
+ document.querySelector(".details .issuerOrganizationUnit").innerHTML =
421
+ certificate.issuerOrganizationUnit;
422
+ document.querySelector(".details .issuerOrganization").innerHTML =
423
+ certificate.issuerOrganization;
424
+ document.querySelector(".details .issuerCountry").innerHTML =
425
+ certificate.issuerCountry;
426
+ document.querySelector(".details .subjectCommonName").innerHTML =
427
+ certificate.subjectCommonName;
428
+ document.querySelector(".details .subjectOrganizationUnit").innerHTML =
429
+ certificate.subjectOrganizationUnit;
430
+ document.querySelector(".details .subjectOrganization").innerHTML =
431
+ certificate.subjectOrganization;
432
+ document.querySelector(".details .subjectCountry").innerHTML =
433
+ certificate.subjectCountry;
434
+ document.querySelector(".details .issued").innerHTML = certificate.issued;
435
+ document.querySelector(".details .expired").innerHTML = certificate.expired;
436
+ document.querySelector(".details .serialNumber").innerHTML =
437
+ certificate.serialNumber;
438
+ document.querySelector(".details .algorithm").innerHTML =
439
+ certificate.algorithm;
440
+
441
+ /*
442
+ if (certificate.modulus !== undefined) {
443
+ document.querySelector(".details .modulus").innerHTML =
444
+ certificate.modulus;
445
+ document.querySelector("#modulusContainers").style.display = "block";
446
+ } else {
447
+ document.querySelector("#modulusContainers").style.display = "none";
448
+ }
449
+ */
450
+
451
+ if (certificate.namedCurve !== undefined) {
452
+ document.querySelector(".details .namedCurve").innerHTML =
453
+ certificate.namedCurve;
454
+ document.querySelector("#curveContainer").style.display = "block";
455
+ } else {
456
+ document.querySelector("#curveContainer").style.display = "none";
457
+ }
458
+ }