|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\SocialPost; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\SocialPostAttachment; |
|
11
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\Node\SocialPostAttachmentRepository; |
|
13
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use DateTimeZone; |
|
16
|
|
|
use DirectoryIterator; |
|
17
|
|
|
use Doctrine\DBAL\Connection; |
|
18
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
20
|
|
|
|
|
21
|
|
|
final class Version20231026231100 extends AbstractMigrationChamilo |
|
22
|
|
|
{ |
|
23
|
|
|
public function getDescription(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return 'Migrate message_attachments to social_post_attachments'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function up(Schema $schema): void |
|
29
|
|
|
{ |
|
30
|
|
|
$container = $this->getContainer(); |
|
31
|
|
|
$em = $this->getEntityManager(); |
|
32
|
|
|
|
|
33
|
|
|
/** @var Connection $connection */ |
|
34
|
|
|
$connection = $em->getConnection(); |
|
35
|
|
|
|
|
36
|
|
|
$kernel = $container->get('kernel'); |
|
37
|
|
|
$rootPath = $kernel->getProjectDir(); |
|
38
|
|
|
|
|
39
|
|
|
$repo = $container->get(SocialPostAttachmentRepository::class); |
|
40
|
|
|
$userRepo = $container->get(UserRepository::class); |
|
41
|
|
|
$admin = $this->getAdmin(); |
|
42
|
|
|
|
|
43
|
|
|
$sub = $em->createQueryBuilder(); |
|
44
|
|
|
$sub->select('sp.id') |
|
45
|
|
|
->from('Chamilo\CoreBundle\Entity\SocialPost', 'sp') |
|
46
|
|
|
; |
|
47
|
|
|
|
|
48
|
|
|
$qb = $em->createQueryBuilder(); |
|
49
|
|
|
$qb->select('ma') |
|
50
|
|
|
->from('Chamilo\CoreBundle\Entity\MessageAttachment', 'ma') |
|
51
|
|
|
->where($qb->expr()->in('ma.message', $sub->getDQL())) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
$query = $qb->getQuery(); |
|
55
|
|
|
$messageAttachments = $query->getResult(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($messageAttachments as $attachment) { |
|
58
|
|
|
$message = $attachment->getMessage(); |
|
59
|
|
|
if ($message) { |
|
60
|
|
|
$messageId = $message->getId(); |
|
61
|
|
|
$filename = $attachment->getFilename(); |
|
62
|
|
|
$rootDir = $rootPath.'/app/upload/users'; |
|
63
|
|
|
$targetFile = $attachment->getPath(); |
|
64
|
|
|
$foundFilePath = $this->findFileRecursively($rootDir, $targetFile); |
|
65
|
|
|
$sender = $message->getSender(); |
|
66
|
|
|
|
|
67
|
|
|
if ($foundFilePath) { |
|
68
|
|
|
error_log("File found in $foundFilePath"); |
|
69
|
|
|
|
|
70
|
|
|
$mimeType = mime_content_type($foundFilePath); |
|
71
|
|
|
$uploadFile = new UploadedFile($foundFilePath, $filename, $mimeType, null, true); |
|
72
|
|
|
|
|
73
|
|
|
$socialPost = $em->getRepository(SocialPost::class)->find($messageId); |
|
74
|
|
|
|
|
75
|
|
|
$attachment = new SocialPostAttachment(); |
|
76
|
|
|
$attachment->setSocialPost($socialPost); |
|
77
|
|
|
$attachment->setPath(uniqid('social_post', true)); |
|
78
|
|
|
$attachment->setFilename($uploadFile->getClientOriginalName()); |
|
79
|
|
|
$attachment->setSize($uploadFile->getSize()); |
|
80
|
|
|
$attachment->setInsertUserId($sender->getId()); |
|
81
|
|
|
$attachment->setInsertDateTime(new DateTime('now', new DateTimeZone('UTC'))); |
|
82
|
|
|
$attachment->setParent($sender); |
|
83
|
|
|
$attachment->addUserLink($sender); |
|
84
|
|
|
$attachment->setCreator($sender); |
|
85
|
|
|
|
|
86
|
|
|
$em->persist($attachment); |
|
87
|
|
|
$em->flush(); |
|
88
|
|
|
|
|
89
|
|
|
$repo->addFile($attachment, $uploadFile); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function findFileRecursively(string $directory, string $targetFile): ?string |
|
96
|
|
|
{ |
|
97
|
|
|
if (!is_dir($directory)) { |
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
foreach (new DirectoryIterator($directory) as $fileInfo) { |
|
102
|
|
|
if ($fileInfo->isDot()) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$filePath = $fileInfo->getPathname(); |
|
107
|
|
|
|
|
108
|
|
|
if ($fileInfo->isDir()) { |
|
109
|
|
|
$result = $this->findFileRecursively($filePath, $targetFile); |
|
110
|
|
|
if (null !== $result) { |
|
111
|
|
|
return $result; |
|
112
|
|
|
} |
|
113
|
|
|
} else { |
|
114
|
|
|
if (str_contains($fileInfo->getFilename(), $targetFile)) { |
|
115
|
|
|
return $filePath; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|