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#file', 'url' => '/api/download/{handle}', 'verb' => 'GET'],
['name' => 'Download#one', 'url' => '/api/stream/{fileid}', 'verb' => 'GET'],
// Config API
['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);
if (null === $file) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
$response = new Http\StreamResponse($file->fopen('rb'));
$response->addHeader('Content-Type', $file->getMimeType());
$response->addHeader('Content-Disposition', 'attachment; filename="'.$file->getName().'"');
$response->addHeader('Content-Length', $file->getSize());
// Get DAV location of file
$userFolder = $this->rootFolder->getUserFolder($file->getOwner()->getUID());
$path = $userFolder->getRelativePath($file->getPath());
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 }));
}
static STREAM_FILE(id: number) {
return tok(gen(`${BASE}/stream/{id}`, { id }));
}
static CONFIG(setting: string) {
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) */
export function getDownloadLink(photo: IPhoto) {
const route = vueroute();
// 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}`;
return API.STREAM_FILE(photo.fileid);
}