TrashbinMigrator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Trashbin\UserMigration;
  25. use OCA\Files_Trashbin\AppInfo\Application;
  26. use OCP\Files\Folder;
  27. use OCP\Files\IRootFolder;
  28. use OCP\Files\NotFoundException;
  29. use OCP\IDBConnection;
  30. use OCP\IL10N;
  31. use OCP\IUser;
  32. use OCP\UserMigration\IExportDestination;
  33. use OCP\UserMigration\IImportSource;
  34. use OCP\UserMigration\IMigrator;
  35. use OCP\UserMigration\ISizeEstimationMigrator;
  36. use OCP\UserMigration\TMigratorBasicVersionHandling;
  37. use OCP\UserMigration\UserMigrationException;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator {
  40. use TMigratorBasicVersionHandling;
  41. protected const PATH_FILES_FOLDER = Application::APP_ID.'/files';
  42. protected const PATH_LOCATIONS_FILE = Application::APP_ID.'/locations.json';
  43. protected IRootFolder $root;
  44. protected IDBConnection $dbc;
  45. protected IL10N $l10n;
  46. public function __construct(
  47. IRootFolder $rootFolder,
  48. IDBConnection $dbc,
  49. IL10N $l10n
  50. ) {
  51. $this->root = $rootFolder;
  52. $this->dbc = $dbc;
  53. $this->l10n = $l10n;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function getEstimatedExportSize(IUser $user): int|float {
  59. $uid = $user->getUID();
  60. try {
  61. $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
  62. if (!$trashbinFolder instanceof Folder) {
  63. return 0;
  64. }
  65. return ceil($trashbinFolder->getSize() / 1024);
  66. } catch (\Throwable $e) {
  67. return 0;
  68. }
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
  74. $output->writeln('Exporting trashbin into ' . Application::APP_ID . '…');
  75. $uid = $user->getUID();
  76. try {
  77. $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
  78. if (!$trashbinFolder instanceof Folder) {
  79. throw new UserMigrationException('/'.$uid.'/files_trashbin is not a folder');
  80. }
  81. $output->writeln("Exporting trashbin files…");
  82. $exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER);
  83. $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($uid);
  84. $exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations));
  85. } catch (NotFoundException $e) {
  86. $output->writeln("No trashbin to export…");
  87. } catch (\Throwable $e) {
  88. throw new UserMigrationException("Could not export trashbin: ".$e->getMessage(), 0, $e);
  89. }
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
  95. if ($importSource->getMigratorVersion($this->getId()) === null) {
  96. $output->writeln('No version for ' . static::class . ', skipping import…');
  97. return;
  98. }
  99. $output->writeln('Importing trashbin from ' . Application::APP_ID . '…');
  100. $uid = $user->getUID();
  101. if ($importSource->pathExists(static::PATH_FILES_FOLDER)) {
  102. try {
  103. $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
  104. if (!$trashbinFolder instanceof Folder) {
  105. throw new UserMigrationException('Could not import trashbin, /'.$uid.'/files_trashbin is not a folder');
  106. }
  107. } catch (NotFoundException $e) {
  108. $trashbinFolder = $this->root->newFolder('/'.$uid.'/files_trashbin');
  109. }
  110. $output->writeln("Importing trashbin files…");
  111. try {
  112. $importSource->copyToFolder($trashbinFolder, static::PATH_FILES_FOLDER);
  113. } catch (\Throwable $e) {
  114. throw new UserMigrationException("Could not import trashbin.", 0, $e);
  115. }
  116. $locations = json_decode($importSource->getFileContents(static::PATH_LOCATIONS_FILE), true, 512, JSON_THROW_ON_ERROR);
  117. $qb = $this->dbc->getQueryBuilder();
  118. $qb->insert('files_trash')
  119. ->values([
  120. 'id' => $qb->createParameter('id'),
  121. 'timestamp' => $qb->createParameter('timestamp'),
  122. 'location' => $qb->createParameter('location'),
  123. 'user' => $qb->createNamedParameter($uid),
  124. ]);
  125. foreach ($locations as $id => $fileLocations) {
  126. foreach ($fileLocations as $timestamp => $location) {
  127. $qb
  128. ->setParameter('id', $id)
  129. ->setParameter('timestamp', $timestamp)
  130. ->setParameter('location', $location)
  131. ;
  132. $qb->executeStatement();
  133. }
  134. }
  135. } else {
  136. $output->writeln("No trashbin to import…");
  137. }
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function getId(): string {
  143. return 'trashbin';
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function getDisplayName(): string {
  149. return $this->l10n->t('Deleted files');
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public function getDescription(): string {
  155. return $this->l10n->t('Deleted files and folders in the trash bin (may expire during export if you are low on storage space)');
  156. }
  157. }