upload method

Future<Uri> upload({
  1. String relativeDirPath = "",
  2. int? limitSize,
})

Upload picked up files directly to storage.

Pass relativeDirPath the relative path of the folder where you want to upload the file.

After uploading, the public URL is returned after the upload.

You can also specify the maximum vertical and horizontal size of the image by passing limitSize. This value is applied if PickerMasamuneAdapter.forceImageResize is true.

ピックアップしたファイルを直接ストレージにアップロードします。

relativeDirPathにファイルをアップロードしたいフォルダの相対パスを渡してください。

アップロードした後、アップロード後の公開URLが返されます。

またlimitSizeを渡すことで、画像の縦および横の最大サイズを指定することが可能です。 PickerMasamuneAdapter.forceImageResizeがtrueの場合、この値が適用されます。

Implementation

Future<Uri> upload({String relativeDirPath = "", int? limitSize}) async {
  relativeDirPath = relativeDirPath.trimQuery().trimString("/");
  final path = uri.toString();
  final adapter = PickerMasamuneAdapter.primary;
  if (uri == null || path.isEmpty) {
    throw Exception("Upload data was not found.");
  }
  if (adapter.forceImageResize) {
    final image = await _decodeImage(path, mimeType?.value);
    if (image != null) {
      final imageResizeLimit =
          adapter.imageResizeLimits.sortTo((a, b) => b.compareTo(a));
      final width = image.width;
      final height = image.height;
      final resizedImage = img.resize(
        image,
        maintainAspect: true,
        interpolation: adapter.imageResizeInterpolation.toInterpolation(),
        width: width >= height
            ? limitSize != null
                ? limitSize <= width
                    ? limitSize
                    : imageResizeLimit.firstWhere((limit) => width >= limit)
                : imageResizeLimit.firstWhere((limit) => width >= limit)
            : null,
        height: height > width
            ? limitSize != null
                ? limitSize <= height
                    ? limitSize
                    : imageResizeLimit.firstWhere((limit) => height >= limit)
                : imageResizeLimit.firstWhere((limit) => height >= limit)
            : null,
      );
      final remoteFile = path.trimQuery().trimStringRight("/").last();
      // .が入っていない場合blobのため拡張子を取得できない
      final extension = remoteFile.contains(".")
          ? remoteFile.last(separator: ".")
          : mimeType?.extension ?? "";
      final storage = Storage(
        StorageQuery(
          "$relativeDirPath/${uuid()}.$extension"
              .trimQuery()
              .trimStringLeft("/"),
          mimeType: mimeType?.value,
        ),
      );
      await storage
          .uploadWithBytes(_encodeImage(resizedImage, mimeType?.value));
      return await storage.fetchPublicURI();
    }
  }
  final remoteFile = path.trimQuery().trimStringRight("/").last();
  // .が入っていない場合blobのため拡張子を取得できない
  final extension = remoteFile.contains(".")
      ? remoteFile.last(separator: ".")
      : mimeType?.extension ?? "";
  final storage = Storage(
    StorageQuery(
      "$relativeDirPath/${uuid()}.$extension".trimQuery().trimStringLeft("/"),
      mimeType: mimeType?.value,
    ),
  );
  await storage.upload(uri!.toString().replaceAll(RegExp("file://"), ""));
  return await storage.fetchPublicURI();
}