Skip to content

Commit

Permalink
ファイルを並行してアップロードする
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Nov 24, 2023
1 parent b09cf54 commit 5e438d0
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 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 @@ -246,50 +246,13 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
throw SpecifiedException("投票期間を入れてや");
}

if (state.files.length > 16) {
throw SpecifiedException("ファイルは16個以下にしてください");
}

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 @@ -312,6 +275,45 @@ class NoteCreateNotifier extends StateNotifier<NoteCreate> {
throw SpecifiedException("連合オフやのによそのサーバーの人がメンションに含まれてるで");
}

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

0 comments on commit 5e438d0

Please sign in to comment.