Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes 1132] Metadata download #1146

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions geonode_mapstore_client/client/js/actions/gndownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Sync geostory components with their live resources on geonode
*/
export const DOWNLOAD_METADATA = 'GEONODE:DOWNLOAD_METADATA';
export const DOWNLOAD_METADATA_COMPLETE = 'GEONODE:DOWNLOAD_METADATA_COMPLETE';

export function downloadMetaData(linkType, pk) {
return {
type: DOWNLOAD_METADATA,
link: linkType,
pk
};
}

export function downloadMetaDataComplete(linkType, pk) {
return {
type: DOWNLOAD_METADATA_COMPLETE,
link: linkType,
pk
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import expect from 'expect';
import { testEpic } from '@mapstore/framework/epics/__tests__/epicTestUtils';
import { downloadMetaData, DOWNLOAD_METADATA_COMPLETE } from '@js/actions/gndownload';
import { gnDownloadMetaData } from '@js/epics/gndownload';

describe('gnDownloadMetaData epic', () => {
beforeEach(done => {
setTimeout(done);
});
afterEach(done => {
setTimeout(done);
});
it('should download metadata', (done) => {
const NUM_ACTIONS = 1;

testEpic(
gnDownloadMetaData,
NUM_ACTIONS,
downloadMetaData('ISO', 1),
(actions) => {
try {
expect(actions.map(({type}) => type)).toEqual([ DOWNLOAD_METADATA_COMPLETE ]);
done();
} catch (e) {
done(e);
}
},
{},
done
);
});

});
46 changes: 46 additions & 0 deletions geonode_mapstore_client/client/js/epics/gndownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import { Observable } from 'rxjs';
import axios from '@mapstore/framework/libs/ajax';
import { saveAs } from 'file-saver';
import { DOWNLOAD_METADATA, downloadMetaDataComplete } from '@js/actions/gndownload';
import {
error as errorNotification
} from '@mapstore/framework/actions/notifications';

export const gnDownloadMetaData = (action$, store) =>
action$.ofType(DOWNLOAD_METADATA)
.switchMap((action) => {
const state = store.getState();
const url = state.gnresource?.data?.links?.find((link) => link.name === action.link).url;
const resourceTitle = state.gnresource?.data?.title;

return Observable
.defer(() => axios.get(url).then((data) => data))
.switchMap(({ data, headers }) => {
if (headers["content-type"] === "application/xml" || headers["content-type"] === "application/xml; charset=UTF-8") {
let xml = String.fromCharCode.apply(null, new Uint8Array(data));
if (xml.indexOf("<ows:ExceptionReport") === 0) {
throw xml;
}
}
saveAs(new Blob([data], { type: headers && headers["content-type"] }), `${resourceTitle}_${action.link.split(' ').join('_')}_Metadata`);
return Observable.of(downloadMetaDataComplete(action.link, action.pk));
})
.catch((error) => {
return Observable.of(
downloadMetaDataComplete(action.link, action.pk),
errorNotification({ title: "gnviewer.cannotPerfomAction", message: error?.data?.message || error?.data?.detail || error?.originalError?.message || "gnviewer.syncErrorDefault" }));
});

});

export default {
gnDownloadMetaData
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { createPlugin } from '@mapstore/framework/utils/PluginsUtils';
import Message from '@mapstore/framework/components/I18N/Message';
import Button from '@js/components/Button';
import { downloadMetaData } from '@js/actions/gndownload';
import { gnDownloadMetaData } from '@js/epics/gndownload';
import Spinner from '@js/components/Spinner';
import gnDownload from '@js/reducers/gndownload';

function DublinCoreDownload({ onDownload, resourcePk, isDownloading }) {
return (
<Button variant="default" onClick={() => onDownload('Dublin Core', resourcePk)}>
{isDownloading && <Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>} <Message msgId="gnviewer.dublinCore" />
</Button>
);
}

const DublinCoreDownloadPlugin = connect(
createSelector([
state => state?.gnresource?.data.pk || null,
state => state?.gnDownload?.downloads?.DublinCore || {}
], (resourcePk, downloadingResources) => ({
resourcePk,
isDownloading: downloadingResources[resourcePk]
})),
{
onDownload: downloadMetaData
}
)(DublinCoreDownload);

DublinCoreDownload.defaultProps = {
onDownload: () => { },
resourcePk: null,
isDownloading: false
};

export default createPlugin('DublinCoreDownload', {
component: () => null,
containers: {
ActionNavbar: {
name: 'DublinCoreDownload',
Component: DublinCoreDownloadPlugin
}
},
epics: { gnDownloadMetaData },
reducers: { gnDownload }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { createPlugin } from '@mapstore/framework/utils/PluginsUtils';
import Message from '@mapstore/framework/components/I18N/Message';
import Button from '@js/components/Button';
import { downloadMetaData } from '@js/actions/gndownload';
import { gnDownloadMetaData } from '@js/epics/gndownload';
import Spinner from '@js/components/Spinner';
import gnDownload from '@js/reducers/gndownload';

function IsoDownload({ onDownload, resourcePk, isDownloading }) {
return (
<Button variant="default" onClick={() => onDownload('ISO', resourcePk)} className="isobutton">
{isDownloading && <Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>} <Message msgId="gnviewer.iso" />
</Button>
);
}

const IsoDownloadPlugin = connect(
createSelector([
state => state?.gnresource?.data.pk || null,
state => state?.gnDownload?.downloads?.ISO || {}
], (resourcePk, downloadingResources) => ({
resourcePk,
isDownloading: downloadingResources[resourcePk]
})),
{
onDownload: downloadMetaData
}
)(IsoDownload);

IsoDownload.defaultProps = {
onDownload: () => { },
resourcePk: null,
isDownloading: false
};


export default createPlugin('IsoDownload', {
component: () => null,
containers: {
ActionNavbar: {
name: 'IsoDownload',
Component: IsoDownloadPlugin
}
},
epics: { gnDownloadMetaData },
reducers: { gnDownload }
});
8 changes: 8 additions & 0 deletions geonode_mapstore_client/client/js/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ export const plugins = {
SyncPlugin: toLazyPlugin(
'Sync',
() => import(/* webpackChunkName: 'plugins/sync-plugin' */ '@js/plugins/Sync')
),
IsoDownloadPlugin: toLazyPlugin(
'IsoDownload',
() => import(/* webpackChunkName: 'plugins/iso-download-plugin' */ '@js/plugins/downloads/IsoDownload')
),
DublinCoreDownloadPlugin: toLazyPlugin(
'DublinCoreDownload',
() => import(/* webpackChunkName: 'plugins/iso-download-plugin' */ '@js/plugins/downloads/DublinCoreDownload')
)
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import expect from 'expect';
import gndownload from '@js/reducers/gndownload';
import {
downloadMetaData,
downloadMetaDataComplete
} from '@js/actions/gndownload';

describe('gndownload reducer', () => {
it('should test downloadMetaData', () => {
const state = gndownload({downloads: {ISO: {}, DublinCore: {}}}, downloadMetaData('ISO', 1));
expect(state).toEqual({
downloads: {
DublinCore: {},
ISO: {
1: true
}
}
});
});
it('should test downloadMetaDataComplete', () => {
const state = gndownload({downloads: {ISO: {}, DublinCore: {}}}, downloadMetaDataComplete('ISO', 1));
expect(state).toEqual({
downloads: {
DublinCore: {},
ISO: {}
}
});
});
});
53 changes: 53 additions & 0 deletions geonode_mapstore_client/client/js/reducers/gndownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022s, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import { DOWNLOAD_METADATA, DOWNLOAD_METADATA_COMPLETE } from '@js/actions/gndownload';

const defaultState = {
downloads: {
ISO: {},
DublinCore: {}
}
};

function gnDownload(state = defaultState, action) {

switch (action.type) {
case DOWNLOAD_METADATA: {
const linkType = action?.link?.split(' ').join('');
return {
...state,
downloads: {
...state.downloads,
[linkType]: {
[action.pk]: true
}
}
};
}
case DOWNLOAD_METADATA_COMPLETE: {
const newState = { ...state };
const linkType = action?.link?.split(' ').join('');
const downloads = newState.downloads[linkType];
delete downloads[action.pk];
return {
...newState,
downloads: {
...newState.downloads,
[linkType]: {
...downloads
}
}
};
}
default:
return state;
}
}

export default gnDownload;
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,22 @@
"name": "FilterLayer"
},
{
"type": "plugin",
"name": "LayerDownload"
"labelId": "gnviewer.download",
"type": "dropdown",
"items": [
{
"type": "plugin",
"name": "IsoDownload"
},
{
"type": "plugin",
"name": "DublinCoreDownload"
},
{
"type": "plugin",
"name": "LayerDownload"
}
]
},
{
"type": "plugin",
Expand Down Expand Up @@ -1153,6 +1167,12 @@
{
"name": "Playback"
},
{
"name": "IsoDownload"
},
{
"name": "DublinCoreDownload"
},
{
"name": "LayerDownload",
"cfg": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@
"embedgeostory": "Betten Sie diese Geostory ein",
"embeddocument": "Betten Sie dieses Dokument ein",
"embeddashboard": "Betten Sie dieses Dashboard ein",
"directLink": "Direkte Verbindung"
"directLink": "Direkte Verbindung",
"iso": "ISO -Metadaten",
"dublinCore": "Dublin Core Metadaten"
}
}
}
Loading