Skip to content

Commit

Permalink
ファイルを並行してアップロードする
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Jan 21, 2024
1 parent 3d111f2 commit d4f5544
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 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
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: "75fb0d09c60491080cf799ad2408f105e2010ba2"
resolved-ref: a412929c642389f865cc74dd285a3ac967757036
url: "https://github.com/shiosyakeyakini-info/misskey_dart.git"
source: git
version: "1.0.0"
Expand Down

0 comments on commit d4f5544

Please sign in to comment.