Skip to content

Commit

Permalink
fix: Added CORS settings for Storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Nov 27, 2023
1 parent dc0236d commit e5adf74
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
81 changes: 81 additions & 0 deletions packages/katana_cli/lib/action/firebase/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class FirebaseInitCliAction extends CliCommand with CliActionMixin {
final flutter = bin.get("flutter", "flutter");
final npm = bin.get("npm", "npm");
final firebaseCommand = bin.get("firebase", "firebase");
final gsutil = bin.get("gsutil", "gsutil");
final flutterfireCommand = bin.get("flutterfire", "flutterfire");
final firebase = context.yaml.getAsMap("firebase");
final github = context.yaml.getAsMap("github");
Expand All @@ -68,6 +69,7 @@ class FirebaseInitCliAction extends CliCommand with CliActionMixin {
final storage = firebase.getAsMap("storage");
// final overwriteStorageRule = storage.get("overwrite_rule", false);
final enabledStorage = storage.get("enable", false);
final enabledCors = storage.get("cors", false);
final enabledHosting = hosting.get("enable", false);
final enableActions = hosting.get("github_actions", false);
final enabledLogger = firebase.getAsMap("logger").get("enable", false);
Expand Down Expand Up @@ -260,6 +262,21 @@ class FirebaseInitCliAction extends CliCommand with CliActionMixin {
await storageProcess.exitCode;
label("Rewriting Rules");
await const FirebaseStorageRulesCliCode().generateFile("storage.rules");
if (enabledCors) {
label("Set the cors.json");
await const FirebaseStorageCorsCliCode().generateFile("cors.json");
await command(
"Run cors.json deploy",
[
gsutil,
"cors",
"set",
"cors.json",
"gs://$projectId.appspot.com",
],
workingDirectory: "firebase",
);
}
}
}
if (enabledHosting) {
Expand Down Expand Up @@ -706,3 +723,67 @@ service firebase.storage {
""";
}
}

/// Firebase Storage cors.json codebase.
///
/// Firebase Storageのcors.jsonのコードベース。
class FirebaseStorageCorsCliCode extends CliCode {
/// Firebase Storage cors.json codebase.
///
/// Firebase Storageのcors.jsonのコードベース。
const FirebaseStorageCorsCliCode();

@override
String get name => "cors";

@override
String get prefix => "cors";

@override
String get directory => "firebase";

@override
String get description =>
"Firebase Storage cors.json codebase. Firebase Storageのcors.jsonのコードベース。";

/// Checks [fileName] and returns true if the file does not exist.
///
/// [fileName]をチェックしファイルが存在しない場合にtrueを返します。
Future<bool> check(String fileName) async {
if (directory.isNotEmpty) {
final dir = Directory(directory);
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
}
final file = File("${directory.isNotEmpty ? "$directory/" : ""}$fileName");
if (!file.existsSync()) {
return true;
}
return false;
}

@override
String import(String path, String baseName, String className) {
return """
""";
}

@override
String header(String path, String baseName, String className) {
return "";
}

@override
String body(String path, String baseName, String className) {
return """
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
""";
}
}
2 changes: 2 additions & 0 deletions packages/katana_cli/lib/command/doctor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const _mainCommands = {
"The `keytool` command does not exist. Please install the JDK from the following and pass the path under the bin file of the installed JDK:\nhttps://jdk.java.net/\n\n`keytool`コマンドが存在しません。下記からJDKをインストールし、インストールしたJDKのbinファイル以下のパスを通してください。\nhttps://jdk.java.net/",
"openssl":
"The command `openssl` does not exist.\n[Windows] Please install from the following and pass through the path:\nhttps://slproweb.com/products/Win32OpenSSL.html\n[Mac] Please install from the following command and pass through the path:\n`brew install openssl`\n\n`openssl`のコマンドが存在しません。\n[Windows]下記からインストールを行ってパスを通してください。\nhttps://slproweb.com/products/Win32OpenSSL.html\n[Mac]下記のコマンドからインストールを行ってパスを通してください。。\n`brew install openssl`",
"gsutil":
"The `gsutil` command does not exist.\nPlease install from the following:\nhttps://cloud.google.com/storage/docs/gsutil_install\n\n`gsutil`コマンドが存在しません。\n下記からインストールを行ってください。\nhttps://cloud.google.com/storage/docs/gsutil_install",
};

final _macOSMainCommand = {
Expand Down
2 changes: 2 additions & 0 deletions packages/katana_cli/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ ${showAllConfig ? """
# Enable Cloud Storage for Firebase.
# Cloud Storage for Firebaseを有効にします。
# CORSで画像等を取得する
storage:
enable: false
cors: false
# Enable Cloud Functions for Firebase.
# Cloud Functions for Firebaseを有効にします。
Expand Down
2 changes: 1 addition & 1 deletion packages/katana_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ packages:
path: "../katana"
relative: true
source: path
version: "2.5.3"
version: "2.6.3"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit e5adf74

Please sign in to comment.