Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5254 from geoadmin/feature_BGDIINF_SB-2052_kml_he…
Browse files Browse the repository at this point in the history
…ader

Changing KML file backend to new service "service-shorlinks"
  • Loading branch information
procrastinatio authored Mar 31, 2022
2 parents 0dee06f + 96ca7e2 commit e0faa15
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 36 deletions.
9 changes: 6 additions & 3 deletions mk/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ LAST_SHOP_URL := $(call lastvalue,shop-url)
FEEDBACK_URL ?= //map.geo.admin.ch
FEEDBACK_TECH_URL ?= //sys-map.prod.bgdi.ch
LAST_FEEDBACK_URL := $(call lastvalue, feedback-url)
PUBLIC_URL ?= //public.geo.admin.ch
PUBLIC_TECH_URL ?= //public.
PUBLIC_URL ?= //sys-public.prod.bgdi.ch
PUBLIC_TECH_URL ?= //sys-public.
LAST_PUBLIC_URL := $(call lastvalue,public-url)
PRINT_URL ?= //print.geo.admin.ch
PRINT_TECH_URL ?= //service-print.
Expand All @@ -79,9 +79,12 @@ QRCODE_PATH ?= /api/qrcode/generate
LAST_QRCODE_PATH := $(call lastvalue,qrcode-path)
PROXY_URL ?= //service-proxy.prod.bgdi.ch
LAST_PROXY_URL := $(call lastvalue,proxy-url)
STORAGE_URL ?= //sys-public.prod.bgdi.ch
STORAGE_TECH_URL ?= //sys-public.prod.bgdi.ch
STORAGE_LAST_URL := $(call lastvalue,storage-url)
PYPI_URL ?= https://pypi.org/simple/
LAST_PYPI_URL := $(call lastvalue,pypi-url)
PUBLIC_URL_REGEXP ?= ^https?:\/\/public\..*\.(bgdi|admin)\.ch\/.*
PUBLIC_URL_REGEXP ?= ^https?:\/\/(sys-)?public\..*\.(bgdi|admin)\.ch\/.*
ADMIN_URL_REGEXP ?= ^(ftp|http|https):\/\/(.*(\.bgdi\.ch|\.geo\.admin\.ch|\.swisstopo\.cloud))
HREF_REGEXP ?= ^\s*(https?|whatsapp|file|s?ftp|blob|mailto):

Expand Down
2 changes: 2 additions & 0 deletions mk/debug.mk
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ define buildpage
--var "qrcode_path=$(QRCODE_PATH)" \
--var "shop_url=$(SHOP_URL)" \
--var "shop_tech_url=$(SHOP_TECH_URL)" \
--var "storage_url=$(STORAGE_URL)" \
--var "storage_tech_url=$(STORAGE_TECH_URL)" \
--var "wms_url=$(WMS_URL)" \
--var "wms_tech_url=$(WMS_TECH_URL)" \
--var "wmts_url=$(WMTS_URL)" \
Expand Down
1 change: 1 addition & 0 deletions mk/help.mk
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ help:
@echo "- QRCODE_URL qrcode URL (build with: $(LAST_QRCODE_URL), current value: $(QRCODE_URL))"
@echo "- QRCODE_PATH qrcode path (build with: $(LAST_QRCODE_PATH), current value: $(QRCODE_PATH))"
@echo "- SHOP_URL Service URL (build with: $(LAST_SHOP_URL), current value: $(SHOP_URL))"
@echo "- STORAGE_URL Storage/KML URL (build with: $(LAST_STORAGE_URL), current value: $(STORAGE_URL))"
@echo "- WMS_URL Service URL (build with $(LAST_WMS_URL), current value: $(WMS_URL))"
@echo "- WMTS_URL Service URL (build with $(LAST_WMTS_URL), current value: $(WMTS_URL))"
@echo "- TERRAIN_URL Service URL (build with: $(LAST_TERRAIN_URL), current value: $(TERRAIN_URL))"
Expand Down
3 changes: 3 additions & 0 deletions mk/last.mk
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ ${PYTHON_VENV}: .build-artefacts/last-pypi-url
.build-artefacts/last-qrcode-path::
$(call cachelastvariable,$@,$(QRCODE_PATH),$(LAST_QRCODE_PATH),qrcode-path)

.build-artefacts/last-storage-url::
$(call cachelastvariable,$@,$(STORAGE_URL),$(LAST_STORAGE_URL),storage-url)

.build-artefacts/last-apache-base-path::
$(call cachelastvariable,$@,$(APACHE_BASE_PATH),$(LAST_APACHE_BASE_PATH),apache-base-path)

Expand Down
1 change: 1 addition & 0 deletions rc_dev
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export PRINT_URL=//service-print.dev.bgdi.ch
export PROXY_URL=//service-proxy.bgdi-dev.swisstopo.cloud
export QRCODE_URL=//sys-map.dev.bgdi.ch
export QRCODE_PATH=/api/qrcode/generate
export STORAGE_URL=//sys-public.dev.bgdi.ch
export ADMIN_URL_REGEXP="^(ftp|http|https):\/\/(.*(\.bgdi\.ch|\.geo\.admin\.ch|\.swisstopo\.cloud)|localhost:[0-9]{1,5})"
export WMS_URL=//wms-bgdi.dev.bgdi.ch
export WMTS_URL=//tod.dev.bgdi.ch
Expand Down
1 change: 1 addition & 0 deletions rc_int
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export PUBLIC_URL=//public.int.bgdi.ch
export PRINT_URL=//service-print.int.bgdi.ch
export PROXY_URL=//service-proxy.int.bgdi.ch
export QRCODE_URL=//sys-map.int.bgdi.ch
export STORAGE_URL=//sys-public.int.bgdi.ch
export WMS_URL=//wms-bgdi-cdn-{s}.int.bgdi.ch
export WMTS_URL=//wmts{s}.bgdi.ch
export TERRAIN_URL=//terrain100.bgdi.ch
Expand Down
1 change: 1 addition & 0 deletions rc_prod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export PUBLIC_URL=//public.geo.admin.ch
export PRINT_URL=//print.geo.admin.ch
export PROXY_URL=//service-proxy.prod.bgdi.ch
export QRCODE_URL=//map.geo.admin.ch
export STORAGE_URL=//sys-public.prod.bgdi.ch
export WMS_URL=//wms{s}.geo.admin.ch
export WMTS_URL=//wmts{s}.geo.admin.ch
export WMTS_TECH_URL=//wmts.
Expand Down
10 changes: 5 additions & 5 deletions src/components/FileStorageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ goog.require('ga_publicstorage_service');
*/
module.provider('gaFileStorage', function() {
this.$get = function($http, $q, gaPublicStorage) {
var endPoint = '/files';
var publicEndPoint = '';
var endPoint = '/api/kml/admin';
var publicEndPoint = '/kml/files';
var contentType = 'application/vnd.google-earth.kml+xml';

var FileStorage = function() {
Expand All @@ -35,9 +35,9 @@ goog.require('ga_publicstorage_service');
adminId);
};

this.save = function(id, content) {
return gaPublicStorage.save(endPoint, publicEndPoint, id, content,
contentType);
this.save = function(fileId, adminId, content) {
return gaPublicStorage.save(endPoint, publicEndPoint, fileId,
adminId, content, contentType);
};

// Delete the file in s3. Only if an adminId is specified
Expand Down
48 changes: 36 additions & 12 deletions src/components/PublicStorageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ goog.provide('ga_publicstorage_service');
this.$get = function($http, $q, gaGlobalOptions) {

var getServiceUrl = function(endPoint, id) {
return gaGlobalOptions.apiUrl + endPoint + ((id) ? '/' + id : '');
return gaGlobalOptions.storageUrl + endPoint + ((id) ? '/' + id : '');
};

var getPublicUrl = function(endPoint, fileId) {
return gaGlobalOptions.publicUrl + endPoint +
return gaGlobalOptions.storageUrl + endPoint +
((fileId) ? '/' + fileId : '');
};

var getMetadataUrl = function(endPoint, adminId) {
return gaGlobalOptions.storageUrl + endPoint +
'?admin_id=' + adminId
}

var PublicStorage = function() {

// Get the file from a fileId
Expand All @@ -35,13 +40,14 @@ goog.provide('ga_publicstorage_service');
};

// Get the accessible url of the file from an adminId
// using the metadata request
this.getFileUrlFromAdminId = function(endPoint, publicEndPoint,
adminId) {
var deferred = $q.defer();
$http.get(getServiceUrl(endPoint, adminId)).then(function(response) {
$http.get(getMetadataUrl(endPoint, adminId)).then(function(response) {
var data = response.data;
if (data && data.fileId) {
var url = getPublicUrl(publicEndPoint, data.fileId);
if (data && data.links && data.links.kml) {
var url = data.links.kml;
deferred.resolve(url);
} else {
deferred.reject();
Expand All @@ -59,18 +65,36 @@ goog.provide('ga_publicstorage_service');
// returns the same adminId and the same file url
// if id is an fileId --> fork the file
// returns new adminId and new file url
this.save = function(endPoint, publicEndPoint, id, content,
contentType) {
return $http.post(getServiceUrl(endPoint, id), content, {
this.save = function(endPoint, publicEndPoint, fileId,
adminId, content, contentType) {

var method = 'PUT'
const formData = new FormData();
const blob = new Blob([content], { name: 'kml',
filename: 'blob',
type: contentType})
formData.append('kml', blob);
formData.append('author', 'mf-geoadmin3');
if (fileId === undefined && adminId === undefined) {
method = 'POST'
} else {
formData.append('admin_id', adminId)
}
return $http({
method: method,
url: getServiceUrl(endPoint, fileId),
data: formData,
// Use 'undefined' to send 'multipart/form-data'
// See https://stackoverflow.com/a/44726531/996693
headers: {
'Content-Type': contentType
'Content-Type': undefined
}
}).then(function(response) {
var data = response.data;
return {
adminId: data.adminId,
fileId: data.fileId,
fileUrl: getPublicUrl(publicEndPoint, data.fileId)
adminId: data.admin_id,
fileId: data.file_id,
fileUrl: ((data || {}).links || {}).kml
};
});
};
Expand Down
7 changes: 4 additions & 3 deletions src/components/draw/DrawDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,10 @@ goog.require('ga_styles_service');
scope.statusMsgId = 'draw_file_saving';
var kmlString = gaExportKml.create(layer,
map.getView().getProjection());
var id = layer.adminId ||
gaFileStorage.getFileIdFromFileUrl(layer.url);
gaFileStorage.save(id, kmlString || '<kml></kml>').then(
var adminId = layer.adminId;
var kmlId = (adminId !== undefined) ?
gaFileStorage.getFileIdFromFileUrl(layer.url) : undefined;
gaFileStorage.save(kmlId, adminId, kmlString || '<kml></kml>').then(
function(data) {
scope.statusMsgId = 'draw_file_saved';

Expand Down
3 changes: 3 additions & 0 deletions src/index.mako.html
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@
var shopUrl = setBackend(prtl + cfg['shop_url'], prtl + cfg['shop_tech_url'], 'shop_url');
var publicUrl = setBackend(prtl + cfg['public_url'], prtl + cfg['public_tech_url'], 'public_url');
var printUrl = setBackend(prtl + cfg['print_url'], prtl + cfg['print_tech_url'], 'print_url');
var storageUrl = setBackend(prtl + cfg['storage_url'], prtl + cfg['storage_tech_url'], 'storage_url');
var proxyUrl = prtl + cfg['proxy_url'] + '/';
var qrcodeUrl = setBackend(prtl + cfg['qrcode_url'], prtl + cfg['qrcode_tech_url'], 'qrcode_url');
var qrcodePath = cfg['qrcode_path'];
Expand Down Expand Up @@ -800,6 +801,8 @@
printUrl: printUrl,
proxyUrl: proxyUrl,
shopUrl: shopUrl,
storageUrl: storageUrl,
storagePath: '/api/kml/admin',
publicUrl: publicUrl,
publicUrlRegexp: new RegExp(cfg['public_url_regexp']),
qrcodeUrl: qrcodeUrl,
Expand Down
3 changes: 2 additions & 1 deletion src/js/GaModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ goog.require('ga_waitcursor_service');
});

module.config(function(gaFileStorageProvider, gaGlobalOptions) {
gaFileStorageProvider.fileStorageUrl = gaGlobalOptions.apiUrl + '/files';
gaFileStorageProvider.fileStorageUrl = gaGlobalOptions.storageUrl +
gaGlobalOptions.storagePath;
gaFileStorageProvider.publicUrl = gaGlobalOptions.publicUrl;
});

Expand Down
25 changes: 13 additions & 12 deletions test/specs/FileStorageService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe('ga_filestorage_service', function() {
adminId = 'aaaaaaaaaaaaaaaaaaaaa', // length must > 20
fileId = 'fffffffffffffffffffff', // length must > 20
fileContent = '<kml></kml>',
blob = new Blob([fileContent]),
form = new FormData(),
fileInfo = {
adminId: adminId,
fileId: fileId,
Expand All @@ -16,14 +18,15 @@ describe('ga_filestorage_service', function() {
fileId: fileId,
fileUrl: 'https://public.geo.admin.ch/' + fileId
},
serviceUrl, publicUrl;
serviceUrl;
form.append('kml', blob)
form.append('author', 'toto')

beforeEach(function() {
inject(function($injector, gaGlobalOptions) {
gaFileStorage = $injector.get('gaFileStorage');
$httpBackend = $injector.get('$httpBackend');
serviceUrl = gaGlobalOptions.apiUrl + '/files';
publicUrl = gaGlobalOptions.publicUrl;
serviceUrl = gaGlobalOptions.storageUrl;
});
});

Expand All @@ -43,7 +46,7 @@ describe('ga_filestorage_service', function() {

describe('#get()', function() {
it('gets a file', function() {
var expectedUrl = publicUrl + '/' + fileId;
var expectedUrl = serviceUrl + '/kml/files/' + fileId;
$httpBackend.expectGET(expectedUrl).respond(fileContent);
gaFileStorage.get(fileId);
$httpBackend.flush();
Expand All @@ -52,24 +55,22 @@ describe('ga_filestorage_service', function() {

describe('#save()', function() {
it('creates a file', inject(function($timeout, gaGlobalOptions) {
$httpBackend.expectPOST(serviceUrl, fileContent).respond(fileInfo);
gaFileStorage.save(null, fileContent);
$httpBackend.expectPOST(serviceUrl + '/api/kml/admin', form).respond(fileInfo);
gaFileStorage.save(undefined, undefined, fileContent);
$httpBackend.flush();
}));

it('updates a file', inject(function($timeout) {
var expectedUrl = serviceUrl + '/' + adminId;
$httpBackend.expectPOST(expectedUrl, fileContent, function(headers) {
return headers['Content-Type'] === 'application/vnd.google-earth.kml+xml';
}).respond(fileInfo);
gaFileStorage.save(adminId, fileContent);
var expectedUrl = serviceUrl + '/api/kml/admin/' + fileId;
$httpBackend.expectPUT(expectedUrl, form).respond(fileInfo);
gaFileStorage.save(fileId, adminId, fileContent);
$httpBackend.flush();
}));
});

describe('#del()', function() {
it('deletes a file', inject(function($timeout) {
var expectedUrl = serviceUrl + '/' + adminId;
var expectedUrl = serviceUrl + '/api/kml/admin/' + adminId;
$httpBackend.expectDELETE(expectedUrl).respond({success: true});
gaFileStorage.del(adminId);
$httpBackend.flush();
Expand Down
2 changes: 2 additions & 0 deletions test/specs/Loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ beforeEach(function() {
var qrcodeUrl = '//sys-map.prod.bgdi.ch';
var qrcodePath = '/api/qrcode/generate';
var shopUrl = '//shop.bgdi.ch';
var storageUrl = '//sys-public.prod.bgdi.ch';
var wmsUrl = '//wms.geo.admin.ch';
var wmtsUrl = '//tod{s}.bgdi.ch';
var terrainUrl = '//terrain100.geo.admin.ch';
Expand Down Expand Up @@ -46,6 +47,7 @@ beforeEach(function() {
qrcodeUrl: location.protocol + qrcodeUrl,
qrcodePath: qrcodePath,
shopUrl: location.protocol + shopUrl,
storageUrl: location.protocol + storageUrl,
publicUrl: location.protocol + publicUrl,
publicUrlRegexp: /^https?:\/\/public\..*\.(bgdi|admin)\.ch\/.*/,
adminUrlRegexp: /^(ftp|http|https):\/\/(.*(\.bgdi|\.geo\.admin)\.ch)/,
Expand Down

0 comments on commit e0faa15

Please sign in to comment.