Skip to content

Commit

Permalink
Merge pull request #60 from linc/fix/vanilla-avatar-filenames
Browse files Browse the repository at this point in the history
Add Vanilla avatar filename processing
  • Loading branch information
linc authored Dec 27, 2024
2 parents c366aac + db400c4 commit bfe318a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/Functions/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,45 @@ function bb_Decodeit($matches)

return "`$text`";
}

/**
* Convert Vanilla's avatar filenames to match the filesystem.
*
* In Vanilla's database, avatars are relative paths, e.g. `userpics/396/YGIC427MJADQ.jpg`
* In the filesystem, filenames are prepended with 'n' (thumbnail - small) or 'p' (profile - fullsize)
* Since we are exporting, assume db values should use the fullsize image's actual filename.
*
* This is a rare case of the intermediary PORTER database necessarily being out of sync with Vanilla's
* due to this one-way data transformation being required.
* Running the export from Vanilla to Vanilla will therefore likely cause avatars to break.
*
* Removes everything from `$path` after final '/', then re-appends the filename
* with 'p' now prepended (if it didn't already start with 'p' to prevent compounding it).
*
* @param string $path
* @return string
*/
function vanillaPhoto($path)
{
// Vanilla can have URLs in the Photo field.
if (strrpos($path, 'http') === 0) {
return $path;
}

// Vanilla Cloud CDN was used & you'll need a bespoke script to fix avatars.
if (strrpos($path, 'static:') === 0) {
return $path;
}

$filename = basename($path);
// Only convert Vanilla-processed avatars (skip previous imports)
// @see Vanilla\FileUtils::generateUniqueUploadPath()
if (preg_match('/[A-Z0-9]{12}\.(jpg|jpeg|gif|png)/', $filename) !== 1) {
return $path;
}
// Don't recursively add 'p' to filenames.
if (strrpos($filename, 'p') !== 0) {
$filename = 'p' . $filename;
}
return substr($path, 0, strrpos($path, '/') + 1) . $filename;
}
12 changes: 11 additions & 1 deletion src/Source/Vanilla.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function run(ExportModel $ex)
'Role',
'Tag',
'TagDiscussion',
'User',
'UserComment',
'UserConversation',
'UserDiscussion',
Expand All @@ -84,6 +83,17 @@ public function run(ExportModel $ex)
$this->polls($ex);
}

/**
* @param ExportModel $ex
*/
public function users(ExportModel $ex)
{
$map = [
'Photo' => ['Column' => 'Photo', 'Type' => 'string', 'Filter' => 'vanillaPhoto'],
];
$ex->export('User', "select * from :_User u", $map);
}

/**
* Badges support for cloud + Yaga.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PorterTest;

use PHPUnit\Framework\TestCase;

final class FilterTest extends TestCase
{
/**
* @covers \vanillaPhoto
*/
public function testParseRender()
{
$photoTests = [
// Add 'p'
'userpics/396/YGIC427MJADQ.jpg' => 'userpics/396/pYGIC427MJADQ.jpg',
'uploads/userpics/820/4J95NK90AFDT.jpeg' => 'uploads/userpics/820/p4J95NK90AFDT.jpeg',
// No change; URL
'https://example.com/forum/uploads/userpics/396/YGIC427MJADQ.jpg'
=> 'https://example.com/forum/uploads/userpics/396/YGIC427MJADQ.jpg',
// No change; not Vanilla origin - filename pattern
'userpics/avatar11_4.gif' => 'userpics/avatar11_4.gif',
// No change; not Vanilla origin - extension
'userpics/396/YGIC427MJADQ.ext' => 'userpics/396/YGIC427MJADQ.ext',
];
foreach ($photoTests as $input => $expectedOutput) {
$testOutput = \vanillaPhoto($input);
$this->assertEquals($testOutput, $expectedOutput);
}
}
}

0 comments on commit bfe318a

Please sign in to comment.