Downloads.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_Sharing\Activity\Providers;
  24. use OCP\Activity\IEvent;
  25. class Downloads extends Base {
  26. const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded';
  27. const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded';
  28. const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded';
  29. const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded';
  30. /**
  31. * @param IEvent $event
  32. * @return IEvent
  33. * @throws \InvalidArgumentException
  34. * @since 11.0.0
  35. */
  36. public function parseShortVersion(IEvent $event) {
  37. $parsedParameters = $this->getParsedParameters($event);
  38. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  39. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  40. $subject = $this->l->t('Downloaded via public link');
  41. } else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  42. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  43. $subject = $this->l->t('Downloaded by {email}');
  44. } else {
  45. throw new \InvalidArgumentException();
  46. }
  47. if ($this->activityManager->getRequirePNG()) {
  48. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  49. } else {
  50. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  51. }
  52. $this->setSubjects($event, $subject, $parsedParameters);
  53. return $event;
  54. }
  55. /**
  56. * @param IEvent $event
  57. * @return IEvent
  58. * @throws \InvalidArgumentException
  59. * @since 11.0.0
  60. */
  61. public function parseLongVersion(IEvent $event) {
  62. $parsedParameters = $this->getParsedParameters($event);
  63. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  64. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  65. $subject = $this->l->t('{file} downloaded via public link');
  66. } else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  67. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  68. $subject = $this->l->t('{email} downloaded {file}');
  69. } else {
  70. throw new \InvalidArgumentException();
  71. }
  72. if ($this->activityManager->getRequirePNG()) {
  73. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  74. } else {
  75. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  76. }
  77. $this->setSubjects($event, $subject, $parsedParameters);
  78. return $event;
  79. }
  80. /**
  81. * @param IEvent $event
  82. * @return array
  83. * @throws \InvalidArgumentException
  84. */
  85. protected function getParsedParameters(IEvent $event) {
  86. $subject = $event->getSubject();
  87. $parameters = $event->getSubjectParameters();
  88. switch ($subject) {
  89. case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
  90. case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
  91. return [
  92. 'file' => $this->getFile($parameters[0], $event),
  93. ];
  94. case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
  95. case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
  96. return [
  97. 'file' => $this->getFile($parameters[0], $event),
  98. 'email' => [
  99. 'type' => 'email',
  100. 'id' => $parameters[1],
  101. 'name' => $parameters[1],
  102. ],
  103. ];
  104. }
  105. throw new \InvalidArgumentException();
  106. }
  107. }