-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathupdate.php
67 lines (56 loc) · 2.17 KB
/
update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* This file is part of the Feeds package.
*
* @author FriendsOfREDAXO
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
$addon = rex_addon::get('feeds');
// Ensure the media directory exists
$mediaPath = rex_path::addonData('feeds', 'media');
if (!is_dir($mediaPath)) {
mkdir($mediaPath, 0777, true);
}
// Add new column if it doesn't exist
rex_sql_table::get(rex::getTable('feeds_item'))
->ensureColumn(new rex_sql_column('media_filename', 'varchar(255)'))
->alter();
// Get all items with media content
$sql = rex_sql::factory();
$items = $sql->getArray('SELECT id, stream_id, uid, media FROM ' . rex::getTable('feeds_item') . ' WHERE media IS NOT NULL AND media != ""');
if ($items) {
foreach ($items as $item) {
try {
// Extract media data
$mediaData = $item['media'];
// Skip if no valid base64 data
if (!preg_match('@^data:image/(.*?);base64,(.+)$@', $mediaData, $matches)) {
continue;
}
// Get file extension from mime type
$extension = $matches[1];
if ($extension === 'jpeg') {
$extension = 'jpg';
}
// Create filename
$filename = sprintf('%d_%s.%s', $item['stream_id'], $item['uid'], $extension);
$filepath = $mediaPath . '/' . $filename;
// Decode and save file
$imageData = base64_decode($matches[2]);
if (file_put_contents($filepath, $imageData)) {
// Update database record
$update = rex_sql::factory();
$update->setTable(rex::getTable('feeds_item'));
$update->setWhere('id = :id', ['id' => $item['id']]);
$update->setValue('media_filename', $filename);
$update->setValue('media', null); // Clear old media data
$update->update();
}
} catch (Exception $e) {
// Log any errors but continue with next item
rex_logger::logException($e);
}
}
}