Skip to content

Commit

Permalink
cleaner code for images without c2pa info and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrks committed Aug 8, 2024
1 parent 82e69bf commit 56a7a51
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 deletions.
52 changes: 35 additions & 17 deletions src/components/NavItems/tools/C2pa/Hooks/useGetC2paData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "redux/reducers/tools/c2paReducer";

/**
*
* This function reads the image's capture information from the assertions
* @param {Array} assertions array containing the assertions of a manifest
* @returns {Object} parsed capture information
*/
Expand Down Expand Up @@ -63,7 +63,7 @@ const exifData = (assertions) => {
};

/**
*
* This function will recursively parse de data from a c2pa manifest and its children, and add it all to the result object
* @param {Object} manifest the c2pa manifest to be read
* @param {string=} parent id of manifest's parent if there is one
* @param {Object} result Object containing the data for the manifests that have already been read
Expand All @@ -72,7 +72,7 @@ const exifData = (assertions) => {
* @returns {Object} contains the id of the manifest that was just read, as well as the updated results
*/
async function readManifest(manifest, parent, result, url, depth) {
// the object that will contain
// the object that will contain the data from this manifest, as well as the parent it
const res = {
url: url,
parent: parent,
Expand Down Expand Up @@ -106,20 +106,33 @@ async function readManifest(manifest, parent, result, url, depth) {
);
let ingredientId;

if (depth < 5 && manifest.ingredients[i].manifest) {
let { id, data } = await readManifest(
manifest.ingredients[i].manifest,
manifestId,
result,
ingredientUrl.url,
depth + 1,
);
ingredientId = id;
result = data;
result[id].validationIssues = validationIssues;
// if a child has more than 5 ancestors, c2pa data will not be shown.
if (depth < 5) {
if (manifest.ingredients[i].manifest) {
let { id, data } = await readManifest(
manifest.ingredients[i].manifest,
manifestId,
result,
ingredientUrl.url,
depth + 1,
);
ingredientId = id;
result = data;
result[id].validationIssues = validationIssues;
} else {
ingredientId = manifest.ingredients[i].instanceId;
result[ingredientId] = {
url: ingredientUrl.url,
parent: manifestId,
};
}
} else {
ingredientId = manifest.ingredients[i].instanceId;
result[ingredientId] = { url: ingredientUrl.url, parent: manifestId };
result[ingredientId] = {
url: ingredientUrl.url,
parent: manifestId,
depthExceeded: true,
};
}
children.push(ingredientId);
manifestData.children = children;
Expand Down Expand Up @@ -232,7 +245,7 @@ async function getC2paData(image, dispatch) {
if (activeManifest) {
let { id, data } = await readManifest(activeManifest, null, {}, url, 0);
data[id].validationIssues = validationIssues;
dispatch(c2paResultSet({ data: data, image: null }));
dispatch(c2paResultSet(data));

// each manifest has an id. The current image id determines which image's data is displayed
// and the main image id is used to return to the first image if a child image is being displayed
Expand All @@ -243,7 +256,12 @@ async function getC2paData(image, dispatch) {
console.log("no active manifest");
}
} else {
dispatch(c2paResultSet({ data: null, image: url }));
// if there is no manifest store, the only data saved for an image is its url
const data = {};
data["id"] = { url: url };
dispatch(c2paResultSet(data));
dispatch(c2paCurrentImageIdSet("id"));
dispatch(c2paMainImageIdSet("id"));
}
dispatch(c2paLoadingSet(false));
} catch (err) {
Expand Down
50 changes: 22 additions & 28 deletions src/components/NavItems/tools/C2pa/Results/C2paResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,23 @@ const C2paResults = (props = { result, handleClose }) => {
const currentImageId = useSelector((state) => state.c2pa.currentImageId);
const mainImageId = useSelector((state) => state.c2pa.mainImageId);

var data;
var img;
var parentId;
var manifestData;
var validationIssues;
var latitude;
var longitude;
const data = props.result;

if (props.result.data) {
data = props.result.data;
const img = data[currentImageId].url;
const parentId = data[currentImageId].parent;
const manifestData = data[currentImageId].manifestData;
const validationIssues = data[currentImageId].validationIssues;

img = data[currentImageId].url;
parentId = data[currentImageId].parent;
manifestData = data[currentImageId].manifestData;
validationIssues = data[currentImageId].validationIssues;
const latitude =
manifestData && manifestData.captureInfo
? manifestData.captureInfo.latitude
: null;
const longitude =
manifestData && manifestData.captureInfo
? manifestData.captureInfo.longitude
: null;

latitude =
manifestData && manifestData.captureInfo
? manifestData.captureInfo.latitude
: null;
longitude =
manifestData && manifestData.captureInfo
? manifestData.captureInfo.longitude
: null;
} else {
img = props.result.image;
}
const depthExceeded = data[currentImageId].depthExceeded;

const dispatch = useDispatch();

Expand Down Expand Up @@ -135,11 +125,15 @@ const C2paResults = (props = { result, handleClose }) => {
<Grid item xs>
<Card p={1}>
<CardContent>
{!data || !manifestData ? (
{!manifestData ? (
<Box m={1}>
<Alert severity="info" m={1}>
{keyword("no_c2pa_info")}
</Alert>
{depthExceeded ? (
<Alert>{keyword("child_depth_exceeded")}</Alert>
) : (
<Alert severity="info" m={1}>
{keyword("no_c2pa_info")}
</Alert>
)}
<Box m={1} />
</Box>
) : (
Expand Down
4 changes: 2 additions & 2 deletions src/constants/tools.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ const c2paData = new Tool(
"navbar_c2pa_description",
c2paSvgIcon,
TOOLS_CATEGORIES.IMAGE,
null,
null,
[TOOL_STATUS_ICON.NEW, TOOL_STATUS_ICON.LOCK],
[ROLES.BETA_TESTER],
"c2pa",
TOOL_GROUPS.VERIFICATION,
<C2paData />,
Expand Down

0 comments on commit 56a7a51

Please sign in to comment.