Downloads.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.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_Sharing\Activity\Providers;
  25. use OCP\Activity\IEvent;
  26. class Downloads extends Base {
  27. public const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded';
  28. public const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded';
  29. public const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded';
  30. public const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded';
  31. /**
  32. * @param IEvent $event
  33. * @return IEvent
  34. * @throws \InvalidArgumentException
  35. * @since 11.0.0
  36. */
  37. public function parseShortVersion(IEvent $event) {
  38. $parsedParameters = $this->getParsedParameters($event);
  39. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  40. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  41. $subject = $this->l->t('Downloaded via public link');
  42. } elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  43. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  44. $subject = $this->l->t('Downloaded by {email}');
  45. } else {
  46. throw new \InvalidArgumentException();
  47. }
  48. if ($this->activityManager->getRequirePNG()) {
  49. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  50. } else {
  51. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  52. }
  53. $this->setSubjects($event, $subject, $parsedParameters);
  54. return $event;
  55. }
  56. /**
  57. * @param IEvent $event
  58. * @param IEvent|null $previousEvent
  59. * @return IEvent
  60. * @throws \InvalidArgumentException
  61. * @since 11.0.0
  62. */
  63. public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
  64. $parsedParameters = $this->getParsedParameters($event);
  65. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  66. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  67. if (!isset($parsedParameters['remote-address-hash']['type'])) {
  68. $subject = $this->l->t('{file} downloaded via public link');
  69. $this->setSubjects($event, $subject, $parsedParameters);
  70. } else {
  71. $subject = $this->l->t('{file} downloaded via public link');
  72. $this->setSubjects($event, $subject, $parsedParameters);
  73. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  74. }
  75. } elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  76. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  77. $subject = $this->l->t('{email} downloaded {file}');
  78. $this->setSubjects($event, $subject, $parsedParameters);
  79. } else {
  80. throw new \InvalidArgumentException();
  81. }
  82. if ($this->activityManager->getRequirePNG()) {
  83. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  84. } else {
  85. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  86. }
  87. return $event;
  88. }
  89. /**
  90. * @param IEvent $event
  91. * @return array
  92. * @throws \InvalidArgumentException
  93. */
  94. protected function getParsedParameters(IEvent $event) {
  95. $subject = $event->getSubject();
  96. $parameters = $event->getSubjectParameters();
  97. switch ($subject) {
  98. case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
  99. case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
  100. if (isset($parameters[1])) {
  101. return [
  102. 'file' => $this->getFile($parameters[0], $event),
  103. 'remote-address-hash' => [
  104. 'type' => 'highlight',
  105. 'id' => $parameters[1],
  106. 'name' => $parameters[1],
  107. 'link' => '',
  108. ],
  109. ];
  110. }
  111. return [
  112. 'file' => $this->getFile($parameters[0], $event),
  113. ];
  114. case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
  115. case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
  116. return [
  117. 'file' => $this->getFile($parameters[0], $event),
  118. 'email' => [
  119. 'type' => 'email',
  120. 'id' => $parameters[1],
  121. 'name' => $parameters[1],
  122. ],
  123. ];
  124. }
  125. throw new \InvalidArgumentException();
  126. }
  127. }