From 926b1bd91f4c1e11ae74959105e08f0c6ffac73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Sun, 9 Aug 2020 16:34:23 +0200 Subject: [PATCH] =?UTF-8?q?feat(convert):=20ajoute=20le=20support=20pour?= =?UTF-8?q?=20les=20documents=20li=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drupal2spip_lal/base/convert.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drupal2spip_lal/base/convert.py b/drupal2spip_lal/base/convert.py index 7c43bc3..6dea37f 100644 --- a/drupal2spip_lal/base/convert.py +++ b/drupal2spip_lal/base/convert.py @@ -56,10 +56,14 @@ def strong_to_dl(html): def download(src, filename, force_download): if src and src.startswith('/'): src = 'http://{}{}'.format(settings.DRUPAL_FQDN, src) + elif src and src.startswith('sites/default/files/'): + src = 'http://{}/{}'.format(settings.DRUPAL_FQDN, src) + elif not re.match(r'^(https?)?://', src): + raise ValueError('Impossible de localiser: {}'.format(src)) path = os.path.join(settings.SPIP_LOGO_DIR, filename) - if not os.access(path, os.R_OK) or force_download: + if not os.access(path, os.F_OK) or force_download: r = request.get(src, stream=True) with open(path, 'wb') as fd: for chunk in r.iter_content(chunk_size=128): @@ -68,8 +72,18 @@ def download(src, filename, force_download): def fetch_document(src, filename, force_download): extension = filename.split('.')[-1] or 'unknown' + directory = os.path.join(settings.SPIP_LOGO_DIR, extension) + if not os.access(directory, os.F_OK): + try: + os.mkdir(directory) + except Exception as e: + logger.warn('Echec à creer le dossier: {}'.format(e)) + cible = os.path.join(extension, filename) - download(src, cible, force_download) + try: + download(src, cible, force_download) + except Exception as e: + logger.warn('Echec au download: {}'.format(e)) return extension, cible @@ -113,7 +127,7 @@ def fetch_and_remove_logo(article, force_download): article.texte = str(soup) article.save() - else: + elif src: logger.warn('Article {} has ignored logo: {}'.format(article.pk, src))