Files
crm.clientright.ru/crm_extensions/file_storage/switch_remaining_to_external.php

93 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* Switch Remaining to External - переключает оставшиеся файлы с S3 метаданными на External
*/
ini_set('memory_limit', '512M');
set_time_limit(0);
date_default_timezone_set('Europe/Moscow');
$ROOT = '/var/www/fastuser/data/www/crm.clientright.ru/';
require_once $ROOT . 'config.inc.php';
// CLI options
$opts = getopt('', [
'dry-run::',
'limit::'
]);
$dryRun = isset($opts['dry-run']) ? (int)$opts['dry-run'] !== 0 : true;
$limit = isset($opts['limit']) ? (int)$opts['limit'] : 0;
// Database connection
$mysqli = new mysqli($dbconfig['db_server'], $dbconfig['db_username'], $dbconfig['db_password'], $dbconfig['db_name']);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$mysqli->set_charset("utf8");
echo "=== Switch Remaining Files to External ===\n";
echo "Dry run: " . ($dryRun ? "YES" : "NO") . "\n";
echo "Limit: " . ($limit > 0 ? $limit : "UNLIMITED") . "\n\n";
// Find Internal files with S3 metadata
$query = "SELECT notesid, filename, s3_key, s3_bucket
FROM vtiger_notes
WHERE filelocationtype = 'I'
AND s3_key IS NOT NULL
AND s3_key != ''";
if ($limit > 0) {
$query .= " LIMIT $limit";
}
$result = $mysqli->query($query);
if (!$result) {
die("Query failed: " . $mysqli->error);
}
$updated_count = 0;
$total_count = 0;
while ($row = $result->fetch_assoc()) {
$total_count++;
$notesid = $row['notesid'];
$filename = $row['filename'];
$s3_key = $row['s3_key'];
$s3_bucket = $row['s3_bucket'] ?: 'f9825c87-4e3558f6-f9b6-405c-ad3d-d1535c49b61c';
echo "Processing ID $notesid: $s3_key\n";
if (!$dryRun) {
// Create S3 URL
$s3_url = "https://s3.twcstorage.ru/{$s3_bucket}/{$s3_key}";
// Update to External type and S3 URL
$update_query = "UPDATE vtiger_notes SET
filelocationtype = 'E',
filename = ?
WHERE notesid = ?";
$stmt = $mysqli->prepare($update_query);
$stmt->bind_param("si", $s3_url, $notesid);
if ($stmt->execute()) {
$updated_count++;
echo " ✅ Switched to External\n";
} else {
echo " ❌ Failed to update: " . $stmt->error . "\n";
}
$stmt->close();
} else {
echo " [DRY RUN] Would switch to External\n";
$updated_count++;
}
}
$mysqli->close();
echo "\n=== SUMMARY ===\n";
echo "Total processed: $total_count\n";
echo "Total updated: $updated_count\n";
echo "Dry run: " . ($dryRun ? "YES" : "NO") . "\n";