Skip to content

Commit

Permalink
fix(server): Force the extention of exported file (#10630)
Browse files Browse the repository at this point in the history
* fix(server): Force the extention of exported file

* update changelog.md
  • Loading branch information
tamaina authored Apr 14, 2023
1 parent 14f30af commit 168fe0e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
-
-->
## 13.x.x (unreleased)

### General
-

### Client
-

### Server
- エクスポートデータの拡張子がunknownになる問題を修正

## 13.11.3

Expand Down
13 changes: 8 additions & 5 deletions packages/backend/src/core/DriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type AddFileArgs = {
uri?: string | null;
/** Mark file as sensitive */
sensitive?: boolean | null;
/** Extension to force */
ext?: string | null;

requestIp?: string | null;
requestHeaders?: Record<string, string> | null;
Expand Down Expand Up @@ -125,7 +127,7 @@ export class DriveService {
/***
* Save file
* @param path Path for original
* @param name Name for original
* @param name Name for original (should be extention corrected)
* @param type Content-Type for original
* @param hash Hash for original
* @param size Size for original
Expand All @@ -151,7 +153,7 @@ export class DriveService {
}

// 拡張子からContent-Typeを設定してそうな挙動を示すオブジェクトストレージ (upcloud?) も存在するので、
// 許可されているファイル形式でしか拡張子をつけない
// 許可されているファイル形式でしかURLに拡張子をつけない
if (!FILE_TYPE_BROWSERSAFE.includes(type)) {
ext = '';
}
Expand All @@ -173,7 +175,7 @@ export class DriveService {
//#region Uploads
this.registerLogger.info(`uploading original: ${key}`);
const uploads = [
this.upload(key, fs.createReadStream(path), type, ext, name),
this.upload(key, fs.createReadStream(path), type, null, name),
];

if (alts.webpublic) {
Expand All @@ -189,7 +191,7 @@ export class DriveService {
thumbnailUrl = `${ baseUrl }/${ thumbnailKey }`;

this.registerLogger.info(`uploading thumbnail: ${thumbnailKey}`);
uploads.push(this.upload(thumbnailKey, alts.thumbnail.data, alts.thumbnail.type, alts.thumbnail.ext));
uploads.push(this.upload(thumbnailKey, alts.thumbnail.data, alts.thumbnail.type, alts.thumbnail.ext, `${name}.thumbnail`));
}

await Promise.all(uploads);
Expand Down Expand Up @@ -443,6 +445,7 @@ export class DriveService {
sensitive = null,
requestIp = null,
requestHeaders = null,
ext = null,
}: AddFileArgs): Promise<DriveFile> {
let skipNsfwCheck = false;
const instance = await this.metaService.fetch();
Expand Down Expand Up @@ -474,7 +477,7 @@ export class DriveService {
// DriveFile.nameは256文字, validateFileNameは200文字制限であるため、
// extを付加してデータベースの文字数制限に当たることはまずない
(name && this.driveFileEntityService.validateFileName(name)) ? name : 'untitled',
info.type.ext,
ext ?? info.type.ext,
);

if (user && !force) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class ExportBlockingProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ExportFavoritesProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'favorites-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.json';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'json' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ExportFollowingProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'following-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ExportMutingProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ExportNotesProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.json';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'json' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ExportUserListsProcessorService {
this.logger.succ(`Exported to: ${path}`);

const fileName = 'user-lists-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });

this.logger.succ(`Exported to: ${driveFile.id}`);
} finally {
Expand Down

0 comments on commit 168fe0e

Please sign in to comment.