You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrashbinMigrator.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = [];
  84. // TODO Export all extra data and bump migrator to v2
  85. foreach (\OCA\Files_Trashbin\Trashbin::getExtraData($uid) as $filename => $extraData) {
  86. $locationData = [];
  87. foreach ($extraData as $timestamp => ['location' => $location]) {
  88. $locationData[$timestamp] = $location;
  89. }
  90. $originalLocations[$filename] = $locationData;
  91. }
  92. $exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations));
  93. } catch (NotFoundException $e) {
  94. $output->writeln("No trashbin to export…");
  95. } catch (\Throwable $e) {
  96. throw new UserMigrationException("Could not export trashbin: ".$e->getMessage(), 0, $e);
  97. }
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
  103. if ($importSource->getMigratorVersion($this->getId()) === null) {
  104. $output->writeln('No version for ' . static::class . ', skipping import…');
  105. return;
  106. }
  107. $output->writeln('Importing trashbin from ' . Application::APP_ID . '…');
  108. $uid = $user->getUID();
  109. if ($importSource->pathExists(static::PATH_FILES_FOLDER)) {
  110. try {
  111. $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
  112. if (!$trashbinFolder instanceof Folder) {
  113. throw new UserMigrationException('Could not import trashbin, /'.$uid.'/files_trashbin is not a folder');
  114. }
  115. } catch (NotFoundException $e) {
  116. $trashbinFolder = $this->root->newFolder('/'.$uid.'/files_trashbin');
  117. }
  118. $output->writeln("Importing trashbin files…");
  119. try {
  120. $importSource->copyToFolder($trashbinFolder, static::PATH_FILES_FOLDER);
  121. } catch (\Throwable $e) {
  122. throw new UserMigrationException("Could not import trashbin.", 0, $e);
  123. }
  124. $locations = json_decode($importSource->getFileContents(static::PATH_LOCATIONS_FILE), true, 512, JSON_THROW_ON_ERROR);
  125. $qb = $this->dbc->getQueryBuilder();
  126. $qb->insert('files_trash')
  127. ->values([
  128. 'id' => $qb->createParameter('id'),
  129. 'timestamp' => $qb->createParameter('timestamp'),
  130. 'location' => $qb->createParameter('location'),
  131. 'user' => $qb->createNamedParameter($uid),
  132. ]);
  133. foreach ($locations as $id => $fileLocations) {
  134. foreach ($fileLocations as $timestamp => $location) {
  135. $qb
  136. ->setParameter('id', $id)
  137. ->setParameter('timestamp', $timestamp)
  138. ->setParameter('location', $location)
  139. ;
  140. $qb->executeStatement();
  141. }
  142. }
  143. } else {
  144. $output->writeln("No trashbin to import…");
  145. }
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. public function getId(): string {
  151. return 'trashbin';
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function getDisplayName(): string {
  157. return $this->l10n->t('Deleted files');
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public function getDescription(): string {
  163. return $this->l10n->t('Deleted files and folders in the trash bin (may expire during export if you are low on storage space)');
  164. }
  165. }