Skip to content

Commit

Permalink
Merge pull request #386 from UN-OCHA/develop
Browse files Browse the repository at this point in the history
v1.0.14
  • Loading branch information
orakili authored Jun 23, 2022
2 parents d36912a + a1e9785 commit eed84b4
Show file tree
Hide file tree
Showing 55 changed files with 975 additions and 1,019 deletions.
20 changes: 20 additions & 0 deletions config/block.block.soft_footer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
uuid: f56298fa-014b-462a-a177-b767582d60c1
langcode: en
status: true
dependencies:
module:
- system
theme:
- common_design_subtheme
id: soft_footer
theme: common_design_subtheme
region: footer_soft
weight: 0
provider: null
plugin: system_powered_by_block
settings:
id: system_powered_by_block
label: 'Soft Footer'
label_display: '0'
provider: system
visibility: { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targetEntityType: media
bundle: image_blog_post
mode: media_library
content:
thumbnail:
field_media_image:
type: image
label: hidden
settings:
Expand All @@ -33,7 +33,7 @@ hidden:
created: true
field_copyright: true
field_description: true
field_media_image: true
langcode: true
name: true
thumbnail: true
uid: true
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targetEntityType: media
bundle: image_book
mode: media_library
content:
thumbnail:
field_media_image:
type: image
label: hidden
settings:
Expand All @@ -33,7 +33,7 @@ hidden:
created: true
field_copyright: true
field_description: true
field_media_image: true
langcode: true
name: true
thumbnail: true
uid: true
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targetEntityType: media
bundle: image_source
mode: media_library
content:
thumbnail:
field_media_image:
type: image
label: hidden
settings:
Expand All @@ -29,7 +29,7 @@ content:
region: content
hidden:
created: true
field_media_image: true
langcode: true
name: true
thumbnail: true
uid: true
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ targetEntityType: media
bundle: image_topic
mode: media_library
content:
thumbnail:
field_media_image:
type: image
label: hidden
settings:
image_link: ''
image_style: icon
svg_attributes:
width: 64
height: 64
width: null
height: null
svg_render_as_image: true
third_party_settings: { }
weight: 0
region: content
hidden:
created: true
field_media_image: true
langcode: true
name: true
thumbnail: true
uid: true
2 changes: 1 addition & 1 deletion config/field.field.node.topic.field_icon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ field_name: field_icon
entity_type: node
bundle: topic
label: Icon
description: 'Use relevant icons from <a href="/report/world/humanitarian-and-country-icons-2018">OCHA''s Humanitarian and Country Icons 2018</a> with modifications in size (600x600) and color (ReliefWeb blue: #0988bb).'
description: ''
required: true
translatable: false
default_value: { }
Expand Down
2 changes: 1 addition & 1 deletion config/social_auth_hid.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ _core:
client_id: CLIENT_ID
client_secret: SECRET
base_url: 'https://auth.humanitarian.id'
auto_redirect: true
auto_redirect: false
disable_default: true
disable_password_fields: true
disable_email_field: false
Expand Down
8 changes: 0 additions & 8 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ RUN rm -rf ./vendor && \
composer selfupdate && \
composer install --no-interaction

# Clean up previous npm installation and run new one. Those commands correspond
# to running `composer sass` but without hitting the PHP timeout from composer.
RUN cd html/themes/custom/common_design_subtheme && \
rm -rf ./node_modules && \
npm install --omit=dev -f && \
./node_modules/.bin/sass sass:build && \
rm -rf ./node_modules

# Copy settings to default site location.
RUN cp -a docker/settings.php docker/services.yml docker/memcache.services.yml html/sites/default

Expand Down
38 changes: 28 additions & 10 deletions docker/etc/nginx/custom/lua/01_legacy_attachment_redirections.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
local attachment_file = ngx.var.attachment_file

-- The generation of the UUID from the legacy URL is similar to what is done
-- in \Drupal\reliefeb_utility\Helpers\LegacyHelper::getAttachmentUuid().
-- Try to get the attachment from a potential symlink.
-- This handles the redirection for some location maps so that they all have
-- the same pattern for example to compensate for some inconsistencies between
-- the URL, URI in DB and file on disk in D7.
-- This can also be used to handle aliases for some specific files.
local symlink = ngx.var.document_root .. '/sites/default/files/legacy-attachments/' .. attachment_file;
local handle = io.popen('readlink "' .. symlink .. '"')
local target = handle:read()
handle:close()

-- Strip the % characters.
attachment_file = string.gsub(attachment_file, "%%", '')
-- If there was no symlink then we generated the target based on the filename.
if target == nil or target == '' then
-- The generation of the UUID from the legacy URL is similar to what is done
-- in \Drupal\reliefeb_utility\Helpers\LegacyHelper::getAttachmentUuid().

-- Consolidate the URL.
local legacy_url = 'https://reliefweb.int/sites/reliefweb.int/files/resources/' .. attachment_file
-- Strip the % characters.
attachment_file = string.gsub(attachment_file, "%%", '')

-- Generate the UUID corresponding to the URL.
local jit_uuid = require 'resty.jit-uuid'
local uuid = jit_uuid.generate_v3('6ba7b811-9dad-11d1-80b4-00c04fd430c8', legacy_url)
-- Consolidate the URL.
local legacy_url = 'https://reliefweb.int/sites/reliefweb.int/files/resources/' .. attachment_file

-- Generate the UUID corresponding to the URL.
local jit_uuid = require 'resty.jit-uuid'
local uuid = jit_uuid.generate_v3('6ba7b811-9dad-11d1-80b4-00c04fd430c8', legacy_url)

target = uuid .. '/' .. attachment_file
else
local uuid = string.gsub(target, '../attachments/[^/]+/[^/]+/([^.]+).+', '%1')
target = uuid .. '/' .. attachment_file
end

-- Redirect to the new URL.
return ngx.redirect('/attachments/' .. uuid .. '/' .. attachment_file, 301)
return ngx.redirect('/attachments/' .. target, 301)
2 changes: 1 addition & 1 deletion html/modules/custom/reliefweb_files/drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
reliefweb_files.commands:
class: \Drupal\reliefweb_files\Commands\ReliefWebFilesCommands
arguments: ['@config.factory', '@database', '@entity_field.manager', '@entity_type.manager', '@reliefweb_files.client']
arguments: ['@config.factory', '@database', '@entity_field.manager', '@entity_type.manager', '@file_system', '@reliefweb_files.client']
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\reliefweb_files\Plugin\Field\FieldType\ReliefWebFile;
use Drupal\reliefweb_files\Services\DocstoreClient;
use Drupal\reliefweb_utility\Helpers\LegacyHelper;
use Drupal\reliefweb_utility\Helpers\UrlHelper;
use Drupal\reliefweb_utility\Traits\EntityDatabaseInfoTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\Uid\Uuid;

/**
* ReliefWeb migration Drush commandfile.
Expand Down Expand Up @@ -52,6 +54,13 @@ class ReliefWebFilesCommands extends DrushCommands {
*/
protected $entityTypeManager;

/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;

/**
* The docstore client service.
*
Expand All @@ -67,12 +76,14 @@ public function __construct(
Connection $database,
EntityFieldManagerInterface $entity_field_manager,
EntityTypeManagerInterface $entity_type_manager,
FileSystemInterface $file_system,
DocstoreClient $docstore_client,
) {
$this->configFactory = $config_factory;
$this->database = $database;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->fileSystem = $file_system;
$this->docstoreClient = $docstore_client;
}

Expand Down Expand Up @@ -896,4 +907,138 @@ public function generateRedirectionSymlinks() {
]));
}

/**
* Generate a symlink of a legacy URL.
*
* This generates a symlink to handle a file whose filename differs from
* its URI basename because the nginx redirection logic cannot work in that
* case as the UUID from the URL will not match the actual UUID of the file.
* So we generate a symlink using the relevant part from the URL to the
* given preview or attachment with the given UUID.
*
* Ex: public://file1.pdf with filename file1_compressed.pdf.
*
* @param string $url
* Legacy attachment or preview URL.
* @param string $uuid
* UUID of the file in the new system.
*
* @command rw-files:generate-redirection-symlink
*
* @usage rw-files:generate-redirection-symlink URL UUID
* Generate the symlink for the URL to the file with the given UUID.
*
* @validate-module-enabled reliefweb_files
*/
public function generateRedirectionSymlink($url, $uuid) {
if (!Uuid::isValid($uuid)) {
$this->logger()->error(dt('Invalid UUID: @uuid', [
'@uuid' => $uuid,
]));
return FALSE;
}

$base_directory = rtrim($this->fileSystem->realpath('public://'), '/');

// Preview.
if (preg_match('#^https?://[^/]+/sites/[^/]+/files/resource-previews/(\d+)-[^/]+$#', $url, $match) === 1) {
$extension = 'png';
$directory = $base_directory . '/legacy-previews';
$target = '../previews/' . substr($uuid, 0, 2) . '/' . substr($uuid, 2, 2) . '/' . $uuid . '.' . $extension;
$link = $directory . '/' . $match[1] . '.' . $extension;
}
// Attachment.
elseif (preg_match('#^https?://[^/]+/sites/[^/]+/files/resources/([^/]+)$#', $url, $match) === 1) {
$extension = ReliefWebFile::extractFileExtension($match[1]);
$directory = $base_directory . '/legacy-attachments';
$target = '../attachments/' . substr($uuid, 0, 2) . '/' . substr($uuid, 2, 2) . '/' . $uuid . '.' . $extension;
$link = $directory . '/' . $match[1];
}
else {
$this->logger()->error(dt('Invalid attachment or preview URL: @url', [
'@url' => $url,
]));
return FALSE;
}

// Create the legacy directory.
if (!$this->fileSystem->prepareDirectory($directory, $this->fileSystem::CREATE_DIRECTORY)) {
$this->logger()->error(dt('Unable to create @directory', [
'@directory' => $directory,
]));
return FALSE;
}

// Remove any previous link.
if (is_link($link)) {
@unlink($link);
}

// Create the symlink.
if (!@symlink($target, $link)) {
$this->logger()->error(dt('Unable to create symlink: @link => @target', [
'@link' => $link,
'@target' => $target,
]));
return FALSE;
}

$this->logger()->success(dt('Successfully created symlink: @link => @target', [
'@link' => $link,
'@target' => $target,
]));
return TRUE;
}

/**
* Remove a symlink of a legacy URL.
*
* @param string $url
* Legacy attachment or preview URL.
*
* @command rw-files:remove-redirection-symlink
*
* @usage rw-files:remove-redirection-symlink
* Remove a legacy symlink.
*
* @validate-module-enabled reliefweb_files
*/
public function removeRedirectionSymlink($url) {
$base_directory = rtrim($this->fileSystem->realpath('public://'), '/');

// Preview.
if (preg_match('#^https?://[^/]+/sites/[^/]+/files/resource-previews/(\d+)-[^/]+$#', $url, $match) === 1) {
$extension = '.png';
$directory = $base_directory . '/legacy-previews';
$link = $directory . '/' . $match[1] . $extension;
}
// Attachment.
elseif (preg_match('#^https?://[^/]+/sites/[^/]+/files/resources/([^/]+)$#', $url, $match) === 1) {
$extension = ReliefWebFile::extractFileExtension($match[1]);
$directory = $base_directory . '/legacy-attachments';
$link = $directory . '/' . $match[1];
}
else {
$this->logger()->error(dt('Invalid attachment or preview URL: @url', [
'@url' => $url,
]));
return FALSE;
}

// Remove any previous link.
if (is_link($link)) {
@unlink($link);

$this->logger()->success(dt('Successfully removed symlink @link', [
'@link' => $link,
]));
}
else {
$this->logger()->notice(dt('Symlink @link didn\'t exist', [
'@link' => $link,
]));
}
return TRUE;
}

}
Loading

0 comments on commit eed84b4

Please sign in to comment.