Completed
Push — master ( ac9323...876185 )
by Morris
16:18
created
apps/files_sharing/lib/Controller/ShareController.php 1 patch
Indentation   +557 added lines, -558 removed lines patch added patch discarded remove patch
@@ -71,566 +71,565 @@
 block discarded – undo
71 71
  */
72 72
 class ShareController extends AuthPublicShareController {
73 73
 
74
-	/** @var IConfig */
75
-	protected $config;
76
-	/** @var IUserManager */
77
-	protected $userManager;
78
-	/** @var ILogger */
79
-	protected $logger;
80
-	/** @var \OCP\Activity\IManager */
81
-	protected $activityManager;
82
-	/** @var IPreview */
83
-	protected $previewManager;
84
-	/** @var IRootFolder */
85
-	protected $rootFolder;
86
-	/** @var FederatedShareProvider */
87
-	protected $federatedShareProvider;
88
-	/** @var EventDispatcherInterface */
89
-	protected $eventDispatcher;
90
-	/** @var IL10N */
91
-	protected $l10n;
92
-	/** @var Defaults */
93
-	protected $defaults;
94
-	/** @var ShareManager */
95
-	protected $shareManager;
96
-
97
-	/** @var Share\IShare */
98
-	protected $share;
99
-
100
-	/**
101
-	 * @param string $appName
102
-	 * @param IRequest $request
103
-	 * @param IConfig $config
104
-	 * @param IURLGenerator $urlGenerator
105
-	 * @param IUserManager $userManager
106
-	 * @param ILogger $logger
107
-	 * @param \OCP\Activity\IManager $activityManager
108
-	 * @param \OCP\Share\IManager $shareManager
109
-	 * @param ISession $session
110
-	 * @param IPreview $previewManager
111
-	 * @param IRootFolder $rootFolder
112
-	 * @param FederatedShareProvider $federatedShareProvider
113
-	 * @param EventDispatcherInterface $eventDispatcher
114
-	 * @param IL10N $l10n
115
-	 * @param Defaults $defaults
116
-	 */
117
-	public function __construct(string $appName,
118
-								IRequest $request,
119
-								IConfig $config,
120
-								IURLGenerator $urlGenerator,
121
-								IUserManager $userManager,
122
-								ILogger $logger,
123
-								\OCP\Activity\IManager $activityManager,
124
-								ShareManager $shareManager,
125
-								ISession $session,
126
-								IPreview $previewManager,
127
-								IRootFolder $rootFolder,
128
-								FederatedShareProvider $federatedShareProvider,
129
-								EventDispatcherInterface $eventDispatcher,
130
-								IL10N $l10n,
131
-								Defaults $defaults) {
132
-		parent::__construct($appName, $request, $session, $urlGenerator);
133
-
134
-		$this->config = $config;
135
-		$this->userManager = $userManager;
136
-		$this->logger = $logger;
137
-		$this->activityManager = $activityManager;
138
-		$this->previewManager = $previewManager;
139
-		$this->rootFolder = $rootFolder;
140
-		$this->federatedShareProvider = $federatedShareProvider;
141
-		$this->eventDispatcher = $eventDispatcher;
142
-		$this->l10n = $l10n;
143
-		$this->defaults = $defaults;
144
-		$this->shareManager = $shareManager;
145
-	}
146
-
147
-	/**
148
-	 * @PublicPage
149
-	 * @NoCSRFRequired
150
-	 *
151
-	 * Show the authentication page
152
-	 * The form has to submit to the authenticate method route
153
-	 */
154
-	public function showAuthenticate(): TemplateResponse {
155
-		$templateParameters = ['share' => $this->share];
156
-
157
-		$event = new GenericEvent(null, $templateParameters);
158
-		$this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);
159
-
160
-		return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
161
-	}
162
-
163
-	/**
164
-	 * The template to show when authentication failed
165
-	 */
166
-	protected function showAuthFailed(): TemplateResponse {
167
-		$templateParameters = ['share' => $this->share, 'wrongpw' => true];
168
-
169
-		$event = new GenericEvent(null, $templateParameters);
170
-		$this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);
171
-
172
-		return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
173
-	}
174
-
175
-	protected function verifyPassword(string $password): bool {
176
-		return $this->shareManager->checkPassword($this->share, $password);
177
-	}
178
-
179
-	protected function getPasswordHash(): string {
180
-		return $this->share->getPassword();
181
-	}
182
-
183
-	public function isValidToken(): bool {
184
-		try {
185
-			$this->share = $this->shareManager->getShareByToken($this->getToken());
186
-		} catch (ShareNotFound $e) {
187
-			return false;
188
-		}
189
-
190
-		return true;
191
-	}
192
-
193
-	protected function isPasswordProtected(): bool {
194
-		return $this->share->getPassword() !== null;
195
-	}
196
-
197
-	protected function authSucceeded() {
198
-		// For share this was always set so it is still used in other apps
199
-		$this->session->set('public_link_authenticated', (string)$this->share->getId());
200
-	}
201
-
202
-	protected function authFailed() {
203
-		$this->emitAccessShareHook($this->share, 403, 'Wrong password');
204
-	}
205
-
206
-	/**
207
-	 * throws hooks when a share is attempted to be accessed
208
-	 *
209
-	 * @param \OCP\Share\IShare|string $share the Share instance if available,
210
-	 * otherwise token
211
-	 * @param int $errorCode
212
-	 * @param string $errorMessage
213
-	 * @throws \OC\HintException
214
-	 * @throws \OC\ServerNotAvailableException
215
-	 */
216
-	protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') {
217
-		$itemType = $itemSource = $uidOwner = '';
218
-		$token = $share;
219
-		$exception = null;
220
-		if($share instanceof \OCP\Share\IShare) {
221
-			try {
222
-				$token = $share->getToken();
223
-				$uidOwner = $share->getSharedBy();
224
-				$itemType = $share->getNodeType();
225
-				$itemSource = $share->getNodeId();
226
-			} catch (\Exception $e) {
227
-				// we log what we know and pass on the exception afterwards
228
-				$exception = $e;
229
-			}
230
-		}
231
-		\OC_Hook::emit(Share::class, 'share_link_access', [
232
-			'itemType' => $itemType,
233
-			'itemSource' => $itemSource,
234
-			'uidOwner' => $uidOwner,
235
-			'token' => $token,
236
-			'errorCode' => $errorCode,
237
-			'errorMessage' => $errorMessage,
238
-		]);
239
-		if(!is_null($exception)) {
240
-			throw $exception;
241
-		}
242
-	}
243
-
244
-	/**
245
-	 * Validate the permissions of the share
246
-	 *
247
-	 * @param Share\IShare $share
248
-	 * @return bool
249
-	 */
250
-	private function validateShare(\OCP\Share\IShare $share) {
251
-		return $share->getNode()->isReadable() && $share->getNode()->isShareable();
252
-	}
253
-
254
-	/**
255
-	 * @PublicPage
256
-	 * @NoCSRFRequired
257
-	 *
258
-
259
-	 * @param string $path
260
-	 * @return TemplateResponse
261
-	 * @throws NotFoundException
262
-	 * @throws \Exception
263
-	 */
264
-	public function showShare($path = ''): TemplateResponse {
265
-		\OC_User::setIncognitoMode(true);
266
-
267
-		// Check whether share exists
268
-		try {
269
-			$share = $this->shareManager->getShareByToken($this->getToken());
270
-		} catch (ShareNotFound $e) {
271
-			$this->emitAccessShareHook($this->getToken(), 404, 'Share not found');
272
-			throw new NotFoundException();
273
-		}
274
-
275
-		if (!$this->validateShare($share)) {
276
-			throw new NotFoundException();
277
-		}
278
-		// We can't get the path of a file share
279
-		try {
280
-			if ($share->getNode() instanceof \OCP\Files\File && $path !== '') {
281
-				$this->emitAccessShareHook($share, 404, 'Share not found');
282
-				throw new NotFoundException();
283
-			}
284
-		} catch (\Exception $e) {
285
-			$this->emitAccessShareHook($share, 404, 'Share not found');
286
-			throw $e;
287
-		}
288
-
289
-		$shareTmpl = [];
290
-		$shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName();
291
-		$shareTmpl['owner'] = $share->getShareOwner();
292
-		$shareTmpl['filename'] = $share->getNode()->getName();
293
-		$shareTmpl['directory_path'] = $share->getTarget();
294
-		$shareTmpl['note'] = $share->getNote();
295
-		$shareTmpl['mimetype'] = $share->getNode()->getMimetype();
296
-		$shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype());
297
-		$shareTmpl['dirToken'] = $this->getToken();
298
-		$shareTmpl['sharingToken'] = $this->getToken();
299
-		$shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
300
-		$shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false';
301
-		$shareTmpl['dir'] = '';
302
-		$shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize();
303
-		$shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize());
304
-
305
-		// Show file list
306
-		$hideFileList = false;
307
-		if ($share->getNode() instanceof \OCP\Files\Folder) {
308
-			/** @var \OCP\Files\Folder $rootFolder */
309
-			$rootFolder = $share->getNode();
310
-
311
-			try {
312
-				$folderNode = $rootFolder->get($path);
313
-			} catch (\OCP\Files\NotFoundException $e) {
314
-				$this->emitAccessShareHook($share, 404, 'Share not found');
315
-				throw new NotFoundException();
316
-			}
317
-
318
-			$shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath());
319
-
320
-			/*
74
+    /** @var IConfig */
75
+    protected $config;
76
+    /** @var IUserManager */
77
+    protected $userManager;
78
+    /** @var ILogger */
79
+    protected $logger;
80
+    /** @var \OCP\Activity\IManager */
81
+    protected $activityManager;
82
+    /** @var IPreview */
83
+    protected $previewManager;
84
+    /** @var IRootFolder */
85
+    protected $rootFolder;
86
+    /** @var FederatedShareProvider */
87
+    protected $federatedShareProvider;
88
+    /** @var EventDispatcherInterface */
89
+    protected $eventDispatcher;
90
+    /** @var IL10N */
91
+    protected $l10n;
92
+    /** @var Defaults */
93
+    protected $defaults;
94
+    /** @var ShareManager */
95
+    protected $shareManager;
96
+
97
+    /** @var Share\IShare */
98
+    protected $share;
99
+
100
+    /**
101
+     * @param string $appName
102
+     * @param IRequest $request
103
+     * @param IConfig $config
104
+     * @param IURLGenerator $urlGenerator
105
+     * @param IUserManager $userManager
106
+     * @param ILogger $logger
107
+     * @param \OCP\Activity\IManager $activityManager
108
+     * @param \OCP\Share\IManager $shareManager
109
+     * @param ISession $session
110
+     * @param IPreview $previewManager
111
+     * @param IRootFolder $rootFolder
112
+     * @param FederatedShareProvider $federatedShareProvider
113
+     * @param EventDispatcherInterface $eventDispatcher
114
+     * @param IL10N $l10n
115
+     * @param Defaults $defaults
116
+     */
117
+    public function __construct(string $appName,
118
+                                IRequest $request,
119
+                                IConfig $config,
120
+                                IURLGenerator $urlGenerator,
121
+                                IUserManager $userManager,
122
+                                ILogger $logger,
123
+                                \OCP\Activity\IManager $activityManager,
124
+                                ShareManager $shareManager,
125
+                                ISession $session,
126
+                                IPreview $previewManager,
127
+                                IRootFolder $rootFolder,
128
+                                FederatedShareProvider $federatedShareProvider,
129
+                                EventDispatcherInterface $eventDispatcher,
130
+                                IL10N $l10n,
131
+                                Defaults $defaults) {
132
+        parent::__construct($appName, $request, $session, $urlGenerator);
133
+
134
+        $this->config = $config;
135
+        $this->userManager = $userManager;
136
+        $this->logger = $logger;
137
+        $this->activityManager = $activityManager;
138
+        $this->previewManager = $previewManager;
139
+        $this->rootFolder = $rootFolder;
140
+        $this->federatedShareProvider = $federatedShareProvider;
141
+        $this->eventDispatcher = $eventDispatcher;
142
+        $this->l10n = $l10n;
143
+        $this->defaults = $defaults;
144
+        $this->shareManager = $shareManager;
145
+    }
146
+
147
+    /**
148
+     * @PublicPage
149
+     * @NoCSRFRequired
150
+     *
151
+     * Show the authentication page
152
+     * The form has to submit to the authenticate method route
153
+     */
154
+    public function showAuthenticate(): TemplateResponse {
155
+        $templateParameters = ['share' => $this->share];
156
+
157
+        $event = new GenericEvent(null, $templateParameters);
158
+        $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);
159
+
160
+        return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
161
+    }
162
+
163
+    /**
164
+     * The template to show when authentication failed
165
+     */
166
+    protected function showAuthFailed(): TemplateResponse {
167
+        $templateParameters = ['share' => $this->share, 'wrongpw' => true];
168
+
169
+        $event = new GenericEvent(null, $templateParameters);
170
+        $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event);
171
+
172
+        return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
173
+    }
174
+
175
+    protected function verifyPassword(string $password): bool {
176
+        return $this->shareManager->checkPassword($this->share, $password);
177
+    }
178
+
179
+    protected function getPasswordHash(): string {
180
+        return $this->share->getPassword();
181
+    }
182
+
183
+    public function isValidToken(): bool {
184
+        try {
185
+            $this->share = $this->shareManager->getShareByToken($this->getToken());
186
+        } catch (ShareNotFound $e) {
187
+            return false;
188
+        }
189
+
190
+        return true;
191
+    }
192
+
193
+    protected function isPasswordProtected(): bool {
194
+        return $this->share->getPassword() !== null;
195
+    }
196
+
197
+    protected function authSucceeded() {
198
+        // For share this was always set so it is still used in other apps
199
+        $this->session->set('public_link_authenticated', (string)$this->share->getId());
200
+    }
201
+
202
+    protected function authFailed() {
203
+        $this->emitAccessShareHook($this->share, 403, 'Wrong password');
204
+    }
205
+
206
+    /**
207
+     * throws hooks when a share is attempted to be accessed
208
+     *
209
+     * @param \OCP\Share\IShare|string $share the Share instance if available,
210
+     * otherwise token
211
+     * @param int $errorCode
212
+     * @param string $errorMessage
213
+     * @throws \OC\HintException
214
+     * @throws \OC\ServerNotAvailableException
215
+     */
216
+    protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') {
217
+        $itemType = $itemSource = $uidOwner = '';
218
+        $token = $share;
219
+        $exception = null;
220
+        if($share instanceof \OCP\Share\IShare) {
221
+            try {
222
+                $token = $share->getToken();
223
+                $uidOwner = $share->getSharedBy();
224
+                $itemType = $share->getNodeType();
225
+                $itemSource = $share->getNodeId();
226
+            } catch (\Exception $e) {
227
+                // we log what we know and pass on the exception afterwards
228
+                $exception = $e;
229
+            }
230
+        }
231
+        \OC_Hook::emit(Share::class, 'share_link_access', [
232
+            'itemType' => $itemType,
233
+            'itemSource' => $itemSource,
234
+            'uidOwner' => $uidOwner,
235
+            'token' => $token,
236
+            'errorCode' => $errorCode,
237
+            'errorMessage' => $errorMessage,
238
+        ]);
239
+        if(!is_null($exception)) {
240
+            throw $exception;
241
+        }
242
+    }
243
+
244
+    /**
245
+     * Validate the permissions of the share
246
+     *
247
+     * @param Share\IShare $share
248
+     * @return bool
249
+     */
250
+    private function validateShare(\OCP\Share\IShare $share) {
251
+        return $share->getNode()->isReadable() && $share->getNode()->isShareable();
252
+    }
253
+
254
+    /**
255
+     * @PublicPage
256
+     * @NoCSRFRequired
257
+     *
258
+     * @param string $path
259
+     * @return TemplateResponse
260
+     * @throws NotFoundException
261
+     * @throws \Exception
262
+     */
263
+    public function showShare($path = ''): TemplateResponse {
264
+        \OC_User::setIncognitoMode(true);
265
+
266
+        // Check whether share exists
267
+        try {
268
+            $share = $this->shareManager->getShareByToken($this->getToken());
269
+        } catch (ShareNotFound $e) {
270
+            $this->emitAccessShareHook($this->getToken(), 404, 'Share not found');
271
+            throw new NotFoundException();
272
+        }
273
+
274
+        if (!$this->validateShare($share)) {
275
+            throw new NotFoundException();
276
+        }
277
+        // We can't get the path of a file share
278
+        try {
279
+            if ($share->getNode() instanceof \OCP\Files\File && $path !== '') {
280
+                $this->emitAccessShareHook($share, 404, 'Share not found');
281
+                throw new NotFoundException();
282
+            }
283
+        } catch (\Exception $e) {
284
+            $this->emitAccessShareHook($share, 404, 'Share not found');
285
+            throw $e;
286
+        }
287
+
288
+        $shareTmpl = [];
289
+        $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName();
290
+        $shareTmpl['owner'] = $share->getShareOwner();
291
+        $shareTmpl['filename'] = $share->getNode()->getName();
292
+        $shareTmpl['directory_path'] = $share->getTarget();
293
+        $shareTmpl['note'] = $share->getNote();
294
+        $shareTmpl['mimetype'] = $share->getNode()->getMimetype();
295
+        $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype());
296
+        $shareTmpl['dirToken'] = $this->getToken();
297
+        $shareTmpl['sharingToken'] = $this->getToken();
298
+        $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
299
+        $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false';
300
+        $shareTmpl['dir'] = '';
301
+        $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize();
302
+        $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize());
303
+
304
+        // Show file list
305
+        $hideFileList = false;
306
+        if ($share->getNode() instanceof \OCP\Files\Folder) {
307
+            /** @var \OCP\Files\Folder $rootFolder */
308
+            $rootFolder = $share->getNode();
309
+
310
+            try {
311
+                $folderNode = $rootFolder->get($path);
312
+            } catch (\OCP\Files\NotFoundException $e) {
313
+                $this->emitAccessShareHook($share, 404, 'Share not found');
314
+                throw new NotFoundException();
315
+            }
316
+
317
+            $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath());
318
+
319
+            /*
321 320
 			 * The OC_Util methods require a view. This just uses the node API
322 321
 			 */
323
-			$freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath());
324
-			if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) {
325
-				$freeSpace = max($freeSpace, 0);
326
-			} else {
327
-				$freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
328
-			}
329
-
330
-			$hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ);
331
-			$maxUploadFilesize = $freeSpace;
332
-
333
-			$folder = new Template('files', 'list', '');
334
-			$folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath()));
335
-			$folder->assign('dirToken', $this->getToken());
336
-			$folder->assign('permissions', \OCP\Constants::PERMISSION_READ);
337
-			$folder->assign('isPublic', true);
338
-			$folder->assign('hideFileList', $hideFileList);
339
-			$folder->assign('publicUploadEnabled', 'no');
340
-			$folder->assign('uploadMaxFilesize', $maxUploadFilesize);
341
-			$folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize));
342
-			$folder->assign('freeSpace', $freeSpace);
343
-			$folder->assign('usedSpacePercent', 0);
344
-			$folder->assign('trash', false);
345
-			$shareTmpl['folder'] = $folder->fetchPage();
346
-		}
347
-
348
-		$shareTmpl['hideFileList'] = $hideFileList;
349
-		$shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName();
350
-		$shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $this->getToken()]);
351
-		$shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $this->getToken()]);
352
-		$shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
353
-		$shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
354
-		$shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024);
355
-		$shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024);
356
-		$shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null);
357
-		$shareTmpl['previewURL'] = $shareTmpl['downloadURL'];
358
-		$ogPreview = '';
359
-		if ($shareTmpl['previewSupported']) {
360
-			$shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview',
361
-				['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]);
362
-			$ogPreview = $shareTmpl['previewImage'];
363
-
364
-			// We just have direct previews for image files
365
-			if ($share->getNode()->getMimePart() === 'image') {
366
-				$shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $this->getToken()]);
367
-
368
-				$ogPreview = $shareTmpl['previewURL'];
369
-
370
-				//Whatapp is kind of picky about their size requirements
371
-				if ($this->request->isUserAgent(['/^WhatsApp/'])) {
372
-					$ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [
373
-						'token' => $this->getToken(),
374
-						'x' => 256,
375
-						'y' => 256,
376
-						'a' => true,
377
-					]);
378
-				}
379
-			}
380
-		} else {
381
-			$shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
382
-			$ogPreview = $shareTmpl['previewImage'];
383
-		}
384
-
385
-		// Load files we need
386
-		\OCP\Util::addScript('files', 'file-upload');
387
-		\OCP\Util::addStyle('files_sharing', 'publicView');
388
-		\OCP\Util::addScript('files_sharing', 'public');
389
-		\OCP\Util::addScript('files_sharing', 'public_note');
390
-		\OCP\Util::addScript('files', 'fileactions');
391
-		\OCP\Util::addScript('files', 'fileactionsmenu');
392
-		\OCP\Util::addScript('files', 'jquery.fileupload');
393
-		\OCP\Util::addScript('files_sharing', 'files_drop');
394
-
395
-		if (isset($shareTmpl['folder'])) {
396
-			// JS required for folders
397
-			\OCP\Util::addStyle('files', 'merged');
398
-			\OCP\Util::addScript('files', 'filesummary');
399
-			\OCP\Util::addScript('files', 'breadcrumb');
400
-			\OCP\Util::addScript('files', 'fileinfomodel');
401
-			\OCP\Util::addScript('files', 'newfilemenu');
402
-			\OCP\Util::addScript('files', 'files');
403
-			\OCP\Util::addScript('files', 'filemultiselectmenu');
404
-			\OCP\Util::addScript('files', 'filelist');
405
-			\OCP\Util::addScript('files', 'keyboardshortcuts');
406
-		}
407
-
408
-		// OpenGraph Support: http://ogp.me/
409
-		\OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]);
410
-		\OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
411
-		\OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]);
412
-		\OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]);
413
-		\OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]);
414
-		\OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]);
415
-
416
-		$this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts');
417
-
418
-		$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
419
-		$csp->addAllowedFrameDomain('\'self\'');
420
-
421
-		$response = new PublicTemplateResponse($this->appName, 'public', $shareTmpl);
422
-		$response->setHeaderTitle($shareTmpl['filename']);
423
-		$response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['displayName']]));
424
-		$response->setHeaderActions([
425
-			new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0),
426
-			new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']),
427
-			new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']),
428
-			new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $shareTmpl['owner'], $shareTmpl['displayName'], $shareTmpl['filename']),
429
-		]);
430
-
431
-		$response->setContentSecurityPolicy($csp);
432
-
433
-		$this->emitAccessShareHook($share);
434
-
435
-		return $response;
436
-	}
437
-
438
-	/**
439
-	 * @PublicPage
440
-	 * @NoCSRFRequired
441
-	 *
442
-	 * @param string $token
443
-	 * @param string $files
444
-	 * @param string $path
445
-	 * @param string $downloadStartSecret
446
-	 * @return void|\OCP\AppFramework\Http\Response
447
-	 * @throws NotFoundException
448
-	 */
449
-	public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') {
450
-		\OC_User::setIncognitoMode(true);
451
-
452
-		$share = $this->shareManager->getShareByToken($token);
453
-
454
-		if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) {
455
-			return new \OCP\AppFramework\Http\DataResponse('Share is read-only');
456
-		}
457
-
458
-		$files_list = null;
459
-		if (!is_null($files)) { // download selected files
460
-			$files_list = json_decode($files);
461
-			// in case we get only a single file
462
-			if ($files_list === null) {
463
-				$files_list = [$files];
464
-			}
465
-			// Just in case $files is a single int like '1234'
466
-			if (!is_array($files_list)) {
467
-				$files_list = [$files_list];
468
-			}
469
-		}
470
-
471
-
472
-		if (!$this->validateShare($share)) {
473
-			throw new NotFoundException();
474
-		}
475
-
476
-		$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
477
-		$originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath());
478
-
479
-
480
-		// Single file share
481
-		if ($share->getNode() instanceof \OCP\Files\File) {
482
-			// Single file download
483
-			$this->singleFileDownloaded($share, $share->getNode());
484
-		}
485
-		// Directory share
486
-		else {
487
-			/** @var \OCP\Files\Folder $node */
488
-			$node = $share->getNode();
489
-
490
-			// Try to get the path
491
-			if ($path !== '') {
492
-				try {
493
-					$node = $node->get($path);
494
-				} catch (NotFoundException $e) {
495
-					$this->emitAccessShareHook($share, 404, 'Share not found');
496
-					return new NotFoundResponse();
497
-				}
498
-			}
499
-
500
-			$originalSharePath = $userFolder->getRelativePath($node->getPath());
501
-
502
-			if ($node instanceof \OCP\Files\File) {
503
-				// Single file download
504
-				$this->singleFileDownloaded($share, $share->getNode());
505
-			} else if (!empty($files_list)) {
506
-				$this->fileListDownloaded($share, $files_list, $node);
507
-			} else {
508
-				// The folder is downloaded
509
-				$this->singleFileDownloaded($share, $share->getNode());
510
-			}
511
-		}
512
-
513
-		/* FIXME: We should do this all nicely in OCP */
514
-		OC_Util::tearDownFS();
515
-		OC_Util::setupFS($share->getShareOwner());
516
-
517
-		/**
518
-		 * this sets a cookie to be able to recognize the start of the download
519
-		 * the content must not be longer than 32 characters and must only contain
520
-		 * alphanumeric characters
521
-		 */
522
-		if (!empty($downloadStartSecret)
523
-			&& !isset($downloadStartSecret[32])
524
-			&& preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) {
525
-
526
-			// FIXME: set on the response once we use an actual app framework response
527
-			setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/');
528
-		}
529
-
530
-		$this->emitAccessShareHook($share);
531
-
532
-		$server_params = array( 'head' => $this->request->getMethod() === 'HEAD' );
533
-
534
-		/**
535
-		 * Http range requests support
536
-		 */
537
-		if (isset($_SERVER['HTTP_RANGE'])) {
538
-			$server_params['range'] = $this->request->getHeader('Range');
539
-		}
540
-
541
-		// download selected files
542
-		if (!is_null($files) && $files !== '') {
543
-			// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
544
-			// after dispatching the request which results in a "Cannot modify header information" notice.
545
-			OC_Files::get($originalSharePath, $files_list, $server_params);
546
-			exit();
547
-		} else {
548
-			// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
549
-			// after dispatching the request which results in a "Cannot modify header information" notice.
550
-			OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params);
551
-			exit();
552
-		}
553
-	}
554
-
555
-	/**
556
-	 * create activity for every downloaded file
557
-	 *
558
-	 * @param Share\IShare $share
559
-	 * @param array $files_list
560
-	 * @param \OCP\Files\Folder $node
561
-	 */
562
-	protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) {
563
-		foreach ($files_list as $file) {
564
-			$subNode = $node->get($file);
565
-			$this->singleFileDownloaded($share, $subNode);
566
-		}
567
-
568
-	}
569
-
570
-	/**
571
-	 * create activity if a single file was downloaded from a link share
572
-	 *
573
-	 * @param Share\IShare $share
574
-	 */
575
-	protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) {
576
-
577
-		$fileId = $node->getId();
578
-
579
-		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
580
-		$userNodeList = $userFolder->getById($fileId);
581
-		$userNode = $userNodeList[0];
582
-		$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
583
-		$userPath = $userFolder->getRelativePath($userNode->getPath());
584
-		$ownerPath = $ownerFolder->getRelativePath($node->getPath());
585
-
586
-		$parameters = [$userPath];
587
-
588
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
589
-			if ($node instanceof \OCP\Files\File) {
590
-				$subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED;
591
-			} else {
592
-				$subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED;
593
-			}
594
-			$parameters[] = $share->getSharedWith();
595
-		} else {
596
-			if ($node instanceof \OCP\Files\File) {
597
-				$subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
598
-			} else {
599
-				$subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
600
-			}
601
-		}
602
-
603
-		$this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath);
604
-
605
-		if ($share->getShareOwner() !== $share->getSharedBy()) {
606
-			$parameters[0] = $ownerPath;
607
-			$this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath);
608
-		}
609
-	}
610
-
611
-	/**
612
-	 * publish activity
613
-	 *
614
-	 * @param string $subject
615
-	 * @param array $parameters
616
-	 * @param string $affectedUser
617
-	 * @param int $fileId
618
-	 * @param string $filePath
619
-	 */
620
-	protected function publishActivity($subject,
621
-										array $parameters,
622
-										$affectedUser,
623
-										$fileId,
624
-										$filePath) {
625
-
626
-		$event = $this->activityManager->generateEvent();
627
-		$event->setApp('files_sharing')
628
-			->setType('public_links')
629
-			->setSubject($subject, $parameters)
630
-			->setAffectedUser($affectedUser)
631
-			->setObject('files', $fileId, $filePath);
632
-		$this->activityManager->publish($event);
633
-	}
322
+            $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath());
323
+            if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) {
324
+                $freeSpace = max($freeSpace, 0);
325
+            } else {
326
+                $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
327
+            }
328
+
329
+            $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ);
330
+            $maxUploadFilesize = $freeSpace;
331
+
332
+            $folder = new Template('files', 'list', '');
333
+            $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath()));
334
+            $folder->assign('dirToken', $this->getToken());
335
+            $folder->assign('permissions', \OCP\Constants::PERMISSION_READ);
336
+            $folder->assign('isPublic', true);
337
+            $folder->assign('hideFileList', $hideFileList);
338
+            $folder->assign('publicUploadEnabled', 'no');
339
+            $folder->assign('uploadMaxFilesize', $maxUploadFilesize);
340
+            $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize));
341
+            $folder->assign('freeSpace', $freeSpace);
342
+            $folder->assign('usedSpacePercent', 0);
343
+            $folder->assign('trash', false);
344
+            $shareTmpl['folder'] = $folder->fetchPage();
345
+        }
346
+
347
+        $shareTmpl['hideFileList'] = $hideFileList;
348
+        $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName();
349
+        $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $this->getToken()]);
350
+        $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $this->getToken()]);
351
+        $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
352
+        $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
353
+        $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024);
354
+        $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024);
355
+        $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null);
356
+        $shareTmpl['previewURL'] = $shareTmpl['downloadURL'];
357
+        $ogPreview = '';
358
+        if ($shareTmpl['previewSupported']) {
359
+            $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview',
360
+                ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]);
361
+            $ogPreview = $shareTmpl['previewImage'];
362
+
363
+            // We just have direct previews for image files
364
+            if ($share->getNode()->getMimePart() === 'image') {
365
+                $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $this->getToken()]);
366
+
367
+                $ogPreview = $shareTmpl['previewURL'];
368
+
369
+                //Whatapp is kind of picky about their size requirements
370
+                if ($this->request->isUserAgent(['/^WhatsApp/'])) {
371
+                    $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [
372
+                        'token' => $this->getToken(),
373
+                        'x' => 256,
374
+                        'y' => 256,
375
+                        'a' => true,
376
+                    ]);
377
+                }
378
+            }
379
+        } else {
380
+            $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
381
+            $ogPreview = $shareTmpl['previewImage'];
382
+        }
383
+
384
+        // Load files we need
385
+        \OCP\Util::addScript('files', 'file-upload');
386
+        \OCP\Util::addStyle('files_sharing', 'publicView');
387
+        \OCP\Util::addScript('files_sharing', 'public');
388
+        \OCP\Util::addScript('files_sharing', 'public_note');
389
+        \OCP\Util::addScript('files', 'fileactions');
390
+        \OCP\Util::addScript('files', 'fileactionsmenu');
391
+        \OCP\Util::addScript('files', 'jquery.fileupload');
392
+        \OCP\Util::addScript('files_sharing', 'files_drop');
393
+
394
+        if (isset($shareTmpl['folder'])) {
395
+            // JS required for folders
396
+            \OCP\Util::addStyle('files', 'merged');
397
+            \OCP\Util::addScript('files', 'filesummary');
398
+            \OCP\Util::addScript('files', 'breadcrumb');
399
+            \OCP\Util::addScript('files', 'fileinfomodel');
400
+            \OCP\Util::addScript('files', 'newfilemenu');
401
+            \OCP\Util::addScript('files', 'files');
402
+            \OCP\Util::addScript('files', 'filemultiselectmenu');
403
+            \OCP\Util::addScript('files', 'filelist');
404
+            \OCP\Util::addScript('files', 'keyboardshortcuts');
405
+        }
406
+
407
+        // OpenGraph Support: http://ogp.me/
408
+        \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]);
409
+        \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
410
+        \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]);
411
+        \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]);
412
+        \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]);
413
+        \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]);
414
+
415
+        $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts');
416
+
417
+        $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
418
+        $csp->addAllowedFrameDomain('\'self\'');
419
+
420
+        $response = new PublicTemplateResponse($this->appName, 'public', $shareTmpl);
421
+        $response->setHeaderTitle($shareTmpl['filename']);
422
+        $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['displayName']]));
423
+        $response->setHeaderActions([
424
+            new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0),
425
+            new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']),
426
+            new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']),
427
+            new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $shareTmpl['owner'], $shareTmpl['displayName'], $shareTmpl['filename']),
428
+        ]);
429
+
430
+        $response->setContentSecurityPolicy($csp);
431
+
432
+        $this->emitAccessShareHook($share);
433
+
434
+        return $response;
435
+    }
436
+
437
+    /**
438
+     * @PublicPage
439
+     * @NoCSRFRequired
440
+     *
441
+     * @param string $token
442
+     * @param string $files
443
+     * @param string $path
444
+     * @param string $downloadStartSecret
445
+     * @return void|\OCP\AppFramework\Http\Response
446
+     * @throws NotFoundException
447
+     */
448
+    public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') {
449
+        \OC_User::setIncognitoMode(true);
450
+
451
+        $share = $this->shareManager->getShareByToken($token);
452
+
453
+        if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) {
454
+            return new \OCP\AppFramework\Http\DataResponse('Share is read-only');
455
+        }
456
+
457
+        $files_list = null;
458
+        if (!is_null($files)) { // download selected files
459
+            $files_list = json_decode($files);
460
+            // in case we get only a single file
461
+            if ($files_list === null) {
462
+                $files_list = [$files];
463
+            }
464
+            // Just in case $files is a single int like '1234'
465
+            if (!is_array($files_list)) {
466
+                $files_list = [$files_list];
467
+            }
468
+        }
469
+
470
+
471
+        if (!$this->validateShare($share)) {
472
+            throw new NotFoundException();
473
+        }
474
+
475
+        $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
476
+        $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath());
477
+
478
+
479
+        // Single file share
480
+        if ($share->getNode() instanceof \OCP\Files\File) {
481
+            // Single file download
482
+            $this->singleFileDownloaded($share, $share->getNode());
483
+        }
484
+        // Directory share
485
+        else {
486
+            /** @var \OCP\Files\Folder $node */
487
+            $node = $share->getNode();
488
+
489
+            // Try to get the path
490
+            if ($path !== '') {
491
+                try {
492
+                    $node = $node->get($path);
493
+                } catch (NotFoundException $e) {
494
+                    $this->emitAccessShareHook($share, 404, 'Share not found');
495
+                    return new NotFoundResponse();
496
+                }
497
+            }
498
+
499
+            $originalSharePath = $userFolder->getRelativePath($node->getPath());
500
+
501
+            if ($node instanceof \OCP\Files\File) {
502
+                // Single file download
503
+                $this->singleFileDownloaded($share, $share->getNode());
504
+            } else if (!empty($files_list)) {
505
+                $this->fileListDownloaded($share, $files_list, $node);
506
+            } else {
507
+                // The folder is downloaded
508
+                $this->singleFileDownloaded($share, $share->getNode());
509
+            }
510
+        }
511
+
512
+        /* FIXME: We should do this all nicely in OCP */
513
+        OC_Util::tearDownFS();
514
+        OC_Util::setupFS($share->getShareOwner());
515
+
516
+        /**
517
+         * this sets a cookie to be able to recognize the start of the download
518
+         * the content must not be longer than 32 characters and must only contain
519
+         * alphanumeric characters
520
+         */
521
+        if (!empty($downloadStartSecret)
522
+            && !isset($downloadStartSecret[32])
523
+            && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) {
524
+
525
+            // FIXME: set on the response once we use an actual app framework response
526
+            setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/');
527
+        }
528
+
529
+        $this->emitAccessShareHook($share);
530
+
531
+        $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' );
532
+
533
+        /**
534
+         * Http range requests support
535
+         */
536
+        if (isset($_SERVER['HTTP_RANGE'])) {
537
+            $server_params['range'] = $this->request->getHeader('Range');
538
+        }
539
+
540
+        // download selected files
541
+        if (!is_null($files) && $files !== '') {
542
+            // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
543
+            // after dispatching the request which results in a "Cannot modify header information" notice.
544
+            OC_Files::get($originalSharePath, $files_list, $server_params);
545
+            exit();
546
+        } else {
547
+            // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
548
+            // after dispatching the request which results in a "Cannot modify header information" notice.
549
+            OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params);
550
+            exit();
551
+        }
552
+    }
553
+
554
+    /**
555
+     * create activity for every downloaded file
556
+     *
557
+     * @param Share\IShare $share
558
+     * @param array $files_list
559
+     * @param \OCP\Files\Folder $node
560
+     */
561
+    protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) {
562
+        foreach ($files_list as $file) {
563
+            $subNode = $node->get($file);
564
+            $this->singleFileDownloaded($share, $subNode);
565
+        }
566
+
567
+    }
568
+
569
+    /**
570
+     * create activity if a single file was downloaded from a link share
571
+     *
572
+     * @param Share\IShare $share
573
+     */
574
+    protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) {
575
+
576
+        $fileId = $node->getId();
577
+
578
+        $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
579
+        $userNodeList = $userFolder->getById($fileId);
580
+        $userNode = $userNodeList[0];
581
+        $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
582
+        $userPath = $userFolder->getRelativePath($userNode->getPath());
583
+        $ownerPath = $ownerFolder->getRelativePath($node->getPath());
584
+
585
+        $parameters = [$userPath];
586
+
587
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
588
+            if ($node instanceof \OCP\Files\File) {
589
+                $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED;
590
+            } else {
591
+                $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED;
592
+            }
593
+            $parameters[] = $share->getSharedWith();
594
+        } else {
595
+            if ($node instanceof \OCP\Files\File) {
596
+                $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
597
+            } else {
598
+                $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
599
+            }
600
+        }
601
+
602
+        $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath);
603
+
604
+        if ($share->getShareOwner() !== $share->getSharedBy()) {
605
+            $parameters[0] = $ownerPath;
606
+            $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath);
607
+        }
608
+    }
609
+
610
+    /**
611
+     * publish activity
612
+     *
613
+     * @param string $subject
614
+     * @param array $parameters
615
+     * @param string $affectedUser
616
+     * @param int $fileId
617
+     * @param string $filePath
618
+     */
619
+    protected function publishActivity($subject,
620
+                                        array $parameters,
621
+                                        $affectedUser,
622
+                                        $fileId,
623
+                                        $filePath) {
624
+
625
+        $event = $this->activityManager->generateEvent();
626
+        $event->setApp('files_sharing')
627
+            ->setType('public_links')
628
+            ->setSubject($subject, $parameters)
629
+            ->setAffectedUser($affectedUser)
630
+            ->setObject('files', $fileId, $filePath);
631
+        $this->activityManager->publish($event);
632
+    }
634 633
 
635 634
 
636 635
 }
Please login to merge, or discard this patch.