strictNullChecks (#4666)

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip
This commit is contained in:
syuilo 2019-04-13 01:43:22 +09:00 committed by GitHub
parent 4ee40c3345
commit 987168b863
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
214 changed files with 939 additions and 785 deletions

View file

@ -55,10 +55,10 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
const url = `${ baseUrl }/${ key }`;
// for alts
let webpublicKey: string = null;
let webpublicUrl: string = null;
let thumbnailKey: string = null;
let thumbnailUrl: string = null;
let webpublicKey: string | null = null;
let webpublicUrl: string | null = null;
let thumbnailKey: string | null = null;
let thumbnailUrl: string | null = null;
//#endregion
//#region Uploads
@ -106,8 +106,8 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
const url = InternalStorage.saveFromPath(accessKey, path);
let thumbnailUrl: string;
let webpublicUrl: string;
let thumbnailUrl: string | null = null;
let webpublicUrl: string | null = null;
if (alts.thumbnail) {
thumbnailUrl = InternalStorage.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data);
@ -143,7 +143,7 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
*/
export async function generateAlts(path: string, type: string, generateWeb: boolean) {
// #region webpublic
let webpublic: IImage;
let webpublic: IImage | null = null;
if (generateWeb) {
logger.info(`creating web image`);
@ -163,7 +163,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
// #endregion webpublic
// #region thumbnail
let thumbnail: IImage;
let thumbnail: IImage | null = null;
if (['image/jpeg', 'image/webp'].includes(type)) {
thumbnail = await ConvertToJpeg(path, 498, 280);
@ -188,7 +188,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
* Upload to ObjectStorage
*/
async function upload(key: string, stream: fs.ReadStream | Buffer, type: string, filename?: string) {
const minio = new Minio.Client(config.drive.config);
const minio = new Minio.Client(config.drive!.config);
const metadata = {
'Content-Type': type,
@ -197,7 +197,7 @@ async function upload(key: string, stream: fs.ReadStream | Buffer, type: string,
if (filename) metadata['Content-Disposition'] = contentDisposition('inline', filename);
await minio.putObject(config.drive.bucket, key, stream, null, metadata);
await minio.putObject(config.drive!.bucket!, key, stream, undefined, metadata);
}
async function deleteOldFile(user: IRemoteUser) {
@ -232,14 +232,14 @@ async function deleteOldFile(user: IRemoteUser) {
export default async function(
user: User,
path: string,
name: string = null,
comment: string = null,
name: string | null = null,
comment: string | null = null,
folderId: any = null,
force: boolean = false,
isLink: boolean = false,
url: string = null,
uri: string = null,
sensitive: boolean = null
url: string | null = null,
uri: string | null = null,
sensitive: boolean | null = null
): Promise<DriveFile> {
// Calc md5 hash
const calcHash = new Promise<string>((res, rej) => {
@ -300,7 +300,7 @@ export default async function(
throw 'no-free-space';
} else {
// (アバターまたはバナーを含まず)最も古いファイルを削除する
deleteOldFile(user);
deleteOldFile(user as IRemoteUser);
}
}
}
@ -378,7 +378,7 @@ export default async function(
file.comment = comment;
file.properties = properties;
file.isLink = isLink;
file.isSensitive = Users.isLocalUser(user) && profile.alwaysMarkNsfw ? true :
file.isSensitive = Users.isLocalUser(user) && profile!.alwaysMarkNsfw ? true :
(sensitive !== null && sensitive !== undefined)
? sensitive
: false;
@ -411,7 +411,7 @@ export default async function(
file = await DriveFiles.findOne({
uri: file.uri,
userId: user.id
});
}) as DriveFile;
} else {
logger.error(e);
throw e;

View file

@ -7,31 +7,31 @@ import { driveChart, perUserDriveChart, instanceChart } from '../chart';
export default async function(file: DriveFile, isExpired = false) {
if (file.storedInternal) {
InternalStorage.del(file.accessKey);
InternalStorage.del(file.accessKey!);
if (file.thumbnailUrl) {
InternalStorage.del(file.thumbnailAccessKey);
InternalStorage.del(file.thumbnailAccessKey!);
}
if (file.webpublicUrl) {
InternalStorage.del(file.webpublicAccessKey);
InternalStorage.del(file.webpublicAccessKey!);
}
} else if (!file.isLink) {
const minio = new Minio.Client(config.drive.config);
const minio = new Minio.Client(config.drive!.config);
await minio.removeObject(config.drive.bucket, file.accessKey);
await minio.removeObject(config.drive!.bucket!, file.accessKey!);
if (file.thumbnailUrl) {
await minio.removeObject(config.drive.bucket, file.thumbnailAccessKey);
await minio.removeObject(config.drive!.bucket!, file.thumbnailAccessKey!);
}
if (file.webpublicUrl) {
await minio.removeObject(config.drive.bucket, file.webpublicAccessKey);
await minio.removeObject(config.drive!.bucket!, file.webpublicAccessKey!);
}
}
// リモートファイル期限切れ削除後は直リンクにする
if (isExpired && file.userHost !== null) {
if (isExpired && file.userHost !== null && file.uri != null) {
DriveFiles.update(file.id, {
isLink: true,
url: file.uri,

View file

@ -2,7 +2,7 @@ import * as sharp from 'sharp';
export type IImage = {
data: Buffer;
ext: string;
ext: string | null;
type: string;
};

View file

@ -1,4 +1,3 @@
import * as URL from 'url';
import create from './add-file';
import { User } from '../../models/entities/user';
import { driveLogger } from './logger';
@ -13,14 +12,14 @@ const logger = driveLogger.createSubLogger('downloader');
export default async (
url: string,
user: User,
folderId: DriveFolder['id'] = null,
uri: string = null,
folderId: DriveFolder['id'] | null = null,
uri: string | null = null,
sensitive = false,
force = false,
link = false
): Promise<DriveFile> => {
let name = URL.parse(url).pathname.split('/').pop();
if (!DriveFiles.validateFileName(name)) {
let name = new URL(url).pathname.split('/').pop() || null;
if (name == null || !DriveFiles.validateFileName(name)) {
name = null;
}
@ -50,6 +49,6 @@ export default async (
if (error) {
throw error;
} else {
return driveFile;
return driveFile!;
}
};