Skip to content

Commit

Permalink
ファイルを並行してアップロードする
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Jan 28, 2024
1 parent 8ff3599 commit 07f95ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
1 change: 1 addition & 0 deletions lib/l10n/app_ja.arb
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@
"pleaseAddVoteChoice": "投票の選択肢を2つ以上入れてや",
"pleaseSpecifyExpirationDate": "投票がいつまでか入れてや",
"pleaseSpecifyExpirationDuration": "投票期間を入れてや",
"tooManyFiles": "ファイルは16個以下にしてください",
"cannotMentionToRemoteInLocalOnlyNote": "連合オフやのによそのサーバーの人がメンションに含まれてるで",
"cannotPublicReplyToPrivateNote": "リプライが{visibility}やから、パブリックにでけへん",
"@cannotPublicReplyToPrivateNote": {
Expand Down
86 changes: 45 additions & 41 deletions lib/state_notifier/note_create_page/note_create_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class EmptyVoteExpireDateException implements NoteCreateException {}

class EmptyVoteExpireDurationException implements NoteCreateException {}

class TooManyFilesException implements NoteCreateException {}

class MentionToRemoteInLocalOnlyNoteException implements NoteCreateException {}

@freezed
Expand Down Expand Up @@ -280,50 +282,13 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
throw EmptyVoteExpireDurationException();
}

if (state.files.length > 16) {
throw TooManyFilesException();
}

try {
state = state.copyWith(isNoteSending: NoteSendStatus.sending);

final fileIds = <String>[];

for (final file in state.files) {
switch (file) {
case PostFile():
Uint8List contents = await file.file.readAsBytes();
if (["image/jpeg", "image/tiff"].contains(file.type)) {
try {
contents =
await FlutterImageCompress.compressWithList(contents);
} catch (e) {
debugPrint("failed to compress file");
}
}
final response = await misskey.drive.files.createAsBinary(
DriveFilesCreateRequest(
force: true,
name: file.fileName,
isSensitive: file.isNsfw,
comment: file.caption,
),
contents,
);
fileIds.add(response.id);
case AlreadyPostedFile():
if (file.isEdited) {
await misskey.drive.files.update(
DriveFilesUpdateRequest(
fileId: file.file.id,
name: file.fileName,
isSensitive: file.isNsfw,
comment: file.caption,
),
);
}
fileIds.add(file.file.id);
}
}

if (!mounted) return;

final nodes = const MfmParser().parse(state.text);
final userList = <MfmMention>[];

Expand All @@ -346,6 +311,45 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
throw MentionToRemoteInLocalOnlyNoteException();
}

final fileIds = await Future.wait(
state.files.map((file) async {
switch (file) {
case PostFile():
Uint8List contents = await file.file.readAsBytes();
if (["image/jpeg", "image/tiff"].contains(file.type)) {
try {
contents =
await FlutterImageCompress.compressWithList(contents);
} catch (e) {
debugPrint("failed to compress file");
}
}
final response = await misskey.drive.files.createAsBinary(
DriveFilesCreateRequest(
force: true,
name: file.fileName,
isSensitive: file.isNsfw,
comment: file.caption,
),
contents,
);
return response.id;
case AlreadyPostedFile():
if (file.isEdited) {
await misskey.drive.files.update(
DriveFilesUpdateRequest(
fileId: file.file.id,
name: file.fileName,
isSensitive: file.isNsfw,
comment: file.caption,
),
);
}
return file.file.id;
}
}),
);

final mentionTargetUsers = [
for (final user in userList)
await misskey.users.showByName(UsersShowByUserNameRequest(
Expand Down
1 change: 1 addition & 0 deletions lib/view/common/error_dialog_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ErrorDialogListener extends ConsumerWidget {
S.of(context).pleaseSpecifyExpirationDate,
EmptyVoteExpireDurationException() =>
S.of(context).pleaseSpecifyExpirationDuration,
TooManyFilesException() => S.of(context).tooManyFiles,
MentionToRemoteInLocalOnlyNoteException() =>
S.of(context).cannotMentionToRemoteInLocalOnlyNote,
};
Expand Down

0 comments on commit 07f95ed

Please sign in to comment.