download: use unified API for single file stream

pull/363/head
Varun Patil 2023-01-17 19:55:17 -08:00
parent 0b40d80854
commit 62ae3c910a
4 changed files with 29 additions and 33 deletions

View File

@ -74,6 +74,7 @@ return [
['name' => 'Download#request', 'url' => '/api/download', 'verb' => 'POST'], ['name' => 'Download#request', 'url' => '/api/download', 'verb' => 'POST'],
['name' => 'Download#file', 'url' => '/api/download/{handle}', 'verb' => 'GET'], ['name' => 'Download#file', 'url' => '/api/download/{handle}', 'verb' => 'GET'],
['name' => 'Download#one', 'url' => '/api/stream/{fileid}', 'verb' => 'GET'],
// Config API // Config API
['name' => 'Other#setUserConfig', 'url' => '/api/config/{key}', 'verb' => 'PUT'], ['name' => 'Other#setUserConfig', 'url' => '/api/config/{key}', 'verb' => 'PUT'],

View File

@ -113,21 +113,37 @@ class DownloadController extends ApiBase
} }
/** /**
* Download a single file. * @NoAdminRequired
*
* @NoCSRFRequired
*
* @PublicPage
*/ */
private function one(int $fileid): Http\Response public function one(int $fileid): Http\Response
{ {
$file = $this->getUserFile($fileid); $file = $this->getUserFile($fileid);
if (null === $file) { if (null === $file) {
return new JSONResponse([], Http::STATUS_NOT_FOUND); return new JSONResponse([], Http::STATUS_NOT_FOUND);
} }
$response = new Http\StreamResponse($file->fopen('rb')); // Get DAV location of file
$response->addHeader('Content-Type', $file->getMimeType()); $userFolder = $this->rootFolder->getUserFolder($file->getOwner()->getUID());
$response->addHeader('Content-Disposition', 'attachment; filename="'.$file->getName().'"'); $path = $userFolder->getRelativePath($file->getPath());
$response->addHeader('Content-Length', $file->getSize());
return $response; // Setup filesystem for owner
\OC_Util::tearDownFS();
\OC_Util::setupFS($file->getOwner()->getUID());
// HEAD and RANGE support
$server_params = ['head' => 'HEAD' === $this->request->getMethod()];
if (isset($_SERVER['HTTP_RANGE'])) {
$server_params['range'] = $this->request->getHeader('Range');
}
// Write file to output and exit
\OC_Files::get(\dirname($path), basename($path), $server_params);
exit;
} }
/** /**

View File

@ -107,6 +107,10 @@ export class API {
return tok(gen(`${BASE}/download/{handle}`, { handle })); return tok(gen(`${BASE}/download/{handle}`, { handle }));
} }
static STREAM_FILE(id: number) {
return tok(gen(`${BASE}/stream/{id}`, { id }));
}
static CONFIG(setting: string) { static CONFIG(setting: string) {
return gen(`${BASE}/config/{setting}`, { setting }); return gen(`${BASE}/config/{setting}`, { setting });
} }

View File

@ -47,30 +47,5 @@ export async function downloadFilesByPhotos(photos: IPhoto[]) {
/** Get URL to download one file (e.g. for video streaming) */ /** Get URL to download one file (e.g. for video streaming) */
export function getDownloadLink(photo: IPhoto) { export function getDownloadLink(photo: IPhoto) {
const route = vueroute(); return API.STREAM_FILE(photo.fileid);
// Check if public
if (route.name === "folder-share") {
const token = <string>route.params.token;
// TODO: allow proper dav access without the need of basic auth
// https://github.com/nextcloud/server/issues/19700
return generateUrl(`/s/${token}/download?path={dirname}&files={basename}`, {
dirname: photo.filename.split("/").slice(0, -1).join("/"),
basename: photo.basename,
});
}
// Check if albums
if (route.name === "albums") {
const fInfos = getAlbumFileInfos(
[photo],
<string>route.params.user,
<string>route.params.name
);
if (fInfos.length) {
return getRootUrl() + `/remote.php/dav${fInfos[0].originalFilename}`;
}
}
return getRootUrl() + `/remote.php/dav${photo.filename}`;
} }