From c281c34b7dbc5e22be41315fbb3c3bda4fcc366d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 18 Apr 2019 11:18:17 +0200 Subject: [PATCH] add support for custom zip upload webservice (#32141) --- .../migrations/0002_auto_20190418_1118.py | 25 +++++++++++++++++++ grandlyon_cartads_cs/models.py | 21 ++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 grandlyon_cartads_cs/migrations/0002_auto_20190418_1118.py diff --git a/grandlyon_cartads_cs/migrations/0002_auto_20190418_1118.py b/grandlyon_cartads_cs/migrations/0002_auto_20190418_1118.py new file mode 100644 index 0000000..9de0772 --- /dev/null +++ b/grandlyon_cartads_cs/migrations/0002_auto_20190418_1118.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.12 on 2019-04-18 09:18 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('grandlyon_cartads_cs', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='glcartadscs', + name='sendfile_ws_dirname', + field=models.CharField(blank=True, max_length=256, verbose_name='Sendfile Webservice Directory Name'), + ), + migrations.AddField( + model_name='glcartadscs', + name='sendfile_ws_url', + field=models.URLField(blank=True, max_length=256, verbose_name='Sendfile Webservice URL'), + ), + ] diff --git a/grandlyon_cartads_cs/models.py b/grandlyon_cartads_cs/models.py index f513065..a029318 100644 --- a/grandlyon_cartads_cs/models.py +++ b/grandlyon_cartads_cs/models.py @@ -16,6 +16,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import base64 +import os + from django.core.cache import cache from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -40,6 +43,12 @@ class GLCartaDSCS(AbstractCartaDSCS): token_url = models.URLField(_('Token URL'), max_length=256) token_authorization = models.CharField(_('Token Authorization'), max_length=128) + sendfile_ws_url = models.URLField( + _('Sendfile Webservice URL'), + max_length=256, blank=True) + sendfile_ws_dirname = models.CharField( + _('Sendfile Webservice Directory Name'), + max_length=256, blank=True) verify_cert = models.BooleanField(default=True, verbose_name=_('Check HTTPS Certificate validity')) @@ -68,3 +77,15 @@ class GLCartaDSCS(AbstractCartaDSCS): cache.set(cache_key, token, timeout) self.logger.debug('new token: %s (timeout %ss)', token, timeout) return token + + def upload_zip(self, zip_filename): + b64_zip = base64.b64encode(open(zip_filename).read()) + chunk_size = 16777216 # 16MB + for n in range(0, len(b64_zip), chunk_size): + resp = self.requests.post(self.sendfile_ws_url, + data={ + 'fileName': self.sendfile_ws_dirname + os.path.basename(zip_filename), + 'b64_fileContent': b64_zip[n:n+chunk_size], + } + ) + resp.raise_for_status()