-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from aXenDeveloper/files_protected
feat(editor): Download secure files form content
- Loading branch information
Showing
65 changed files
with
862 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
backend/plugins/core/admin/files/download/download.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { createReadStream, existsSync, unlinkSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
import { | ||
Controller, | ||
Get, | ||
Param, | ||
Req, | ||
Res, | ||
StreamableFile | ||
} from "@nestjs/common"; | ||
import { Request, Response } from "express"; | ||
|
||
import { DatabaseService } from "@/plugins/database/database.service"; | ||
import { AuthorizationAdminSessionsService } from "../../sessions/authorization/authorization.service"; | ||
import { InternalAuthorizationCoreSessionsService } from "@/plugins/core/sessions/authorization/internal/internal_authorization.service"; | ||
|
||
@Controller("files") | ||
export class DownloadFilesAdminController { | ||
constructor( | ||
private service: InternalAuthorizationCoreSessionsService, | ||
private readonly serviceAdmin: AuthorizationAdminSessionsService, | ||
private databaseService: DatabaseService | ||
) {} | ||
|
||
@Get(":file") | ||
async getFile( | ||
@Res({ passthrough: true }) res: Response, | ||
@Req() req: Request, | ||
@Param() { file }: { file: string } | ||
): Promise<StreamableFile> { | ||
const path = join(process.cwd(), "temp", file); | ||
if (!existsSync(path)) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
const userId = file.split(".")[0].split("--")[1].split("-")[0]; | ||
const currentFile = { | ||
name: file.split("--")[0], | ||
type: file.split(".")[1] | ||
}; | ||
|
||
const user = await this.databaseService.db.query.core_users.findFirst({ | ||
where: (table, { eq }) => eq(table.id, +userId) | ||
}); | ||
|
||
if (!user) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
|
||
const isAdmin = | ||
await this.databaseService.db.query.core_admin_permissions.findFirst({ | ||
where: (table, { eq, or }) => | ||
or(eq(table.user_id, +userId), eq(table.group_id, user.group_id)) | ||
}); | ||
|
||
if (isAdmin) { | ||
try { | ||
const data = await this.serviceAdmin.initialAuthorization({ | ||
req, | ||
res | ||
}); | ||
|
||
if (+userId !== data.id) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
} catch (e) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
} else { | ||
try { | ||
const data = await this.service.authorization({ | ||
req, | ||
res | ||
}); | ||
|
||
if (+userId !== data.id) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
} catch (e) { | ||
res.status(404); | ||
|
||
return; | ||
} | ||
} | ||
|
||
const streamFile = createReadStream(path); | ||
res.set({ | ||
"Content-Type": `application/${currentFile.type}`, | ||
"Content-Disposition": `attachment; filename="${currentFile.name}.${currentFile.type}"` | ||
}); | ||
|
||
streamFile.on("close", () => { | ||
unlinkSync(path); | ||
}); | ||
|
||
return new StreamableFile(streamFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 17 additions & 77 deletions
94
backend/plugins/core/files/download/download.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.