Revisão | 126 (tree) |
---|---|
Hora | 2022-10-29 15:54:59 |
Autor | ![]() |
0.9.1
FirebaseConfig, FirebaseUploader 外部ファイル config.json を指定できるようにしました。
@@ -14,6 +14,7 @@ | ||
14 | 14 | |
15 | 15 | private String siteId; |
16 | 16 | private Path serviceAccountKeyFilePath; |
17 | + private Path firebaseConfigFilePath; | |
17 | 18 | |
18 | 19 | public FirebaseConfig() { |
19 | 20 | } |
@@ -26,6 +27,10 @@ | ||
26 | 27 | return this.serviceAccountKeyFilePath; |
27 | 28 | } |
28 | 29 | |
30 | + public Path getFirebaseConfigFilePath() { | |
31 | + return this.firebaseConfigFilePath; | |
32 | + } | |
33 | + | |
29 | 34 | private void initialize() { |
30 | 35 | String s; |
31 | 36 |
@@ -37,17 +42,31 @@ | ||
37 | 42 | |
38 | 43 | s = getValueAsString("serviceAccountKey"); |
39 | 44 | if(s != null) { |
40 | - String secretKey = s.replace('/', '\\'); | |
41 | - if(secretKey.length() >= 3 && secretKey.substring(1, 3).equals(":\\")) { | |
42 | - this.serviceAccountKeyFilePath = Paths.get(secretKey).toAbsolutePath(); | |
45 | + String serviceAccountKey = s.replace('/', '\\'); | |
46 | + if(serviceAccountKey.length() >= 3 && serviceAccountKey.substring(1, 3).equals(":\\")) { | |
47 | + this.serviceAccountKeyFilePath = Paths.get(serviceAccountKey).toAbsolutePath(); | |
43 | 48 | } else { |
44 | 49 | Path p = getFolderPath("serviceAccountKey"); |
45 | - this.serviceAccountKeyFilePath = p.resolve(secretKey).toAbsolutePath(); | |
50 | + this.serviceAccountKeyFilePath = p.resolve(serviceAccountKey).toAbsolutePath(); | |
46 | 51 | } |
47 | 52 | if(!Files.exists(this.serviceAccountKeyFilePath)) { |
48 | 53 | throw new UncheckedIOException(new FileNotFoundException(this.serviceAccountKeyFilePath.toString())); |
49 | 54 | } |
50 | 55 | } |
56 | + | |
57 | + s = getValueAsString("config"); | |
58 | + if(s != null) { | |
59 | + String path = s.replace('/', '\\'); | |
60 | + if(path.length() >= 3 && path.substring(1, 3).equals(":\\")) { | |
61 | + this.firebaseConfigFilePath = Paths.get(path).toAbsolutePath(); | |
62 | + } else { | |
63 | + Path folder = getFolderPath("config"); | |
64 | + this.firebaseConfigFilePath = folder.resolve(path).toAbsolutePath(); | |
65 | + } | |
66 | + if(!Files.exists(this.firebaseConfigFilePath)) { | |
67 | + throw new UncheckedIOException((new FileNotFoundException(this.firebaseConfigFilePath.toString()))); | |
68 | + } | |
69 | + } | |
51 | 70 | } |
52 | 71 | |
53 | 72 | public int upload(File dir, ProgressObserver observer) throws Exception { |
@@ -65,6 +65,23 @@ | ||
65 | 65 | throw new ToastMessage("Firebase Hosting", "serviceAccountKey が指定されていません"); |
66 | 66 | } |
67 | 67 | |
68 | + String firebaseConfig; | |
69 | + Path firebaseConfigFilePath = config.getFirebaseConfigFilePath(); | |
70 | + if(firebaseConfigFilePath != null && Files.exists(firebaseConfigFilePath)) { | |
71 | + firebaseConfig = Files.readString(firebaseConfigFilePath); | |
72 | + } else { | |
73 | + firebaseConfig = """ | |
74 | + { | |
75 | + "headers": [{ | |
76 | + "glob": "**", | |
77 | + "headers": { | |
78 | + "Cache-Control": "max-age=1800" | |
79 | + } | |
80 | + }] | |
81 | + } | |
82 | + """; | |
83 | + } | |
84 | + | |
68 | 85 | Path input = localDirectory.toPath(); |
69 | 86 | Path output = MainApp.createTemporaryDirectory("upload-htdocs-gzipped", true); |
70 | 87 |
@@ -76,7 +93,7 @@ | ||
76 | 93 | .executor(executor) |
77 | 94 | .build(); |
78 | 95 | |
79 | - String versionId = createVersionId(client, token, siteId); | |
96 | + String versionId = createVersionId(client, token, siteId, firebaseConfig); | |
80 | 97 | |
81 | 98 | Map<Path, String> files = new LinkedHashMap<>(); |
82 | 99 | int len; |
@@ -157,7 +174,7 @@ | ||
157 | 174 | } |
158 | 175 | |
159 | 176 | |
160 | - private static String createVersionId(HttpClient client, String token, String siteId) throws IOException, InterruptedException { | |
177 | + private static String createVersionId(HttpClient client, String token, String siteId, String firebaseConfig) throws IOException, InterruptedException { | |
161 | 178 | String versionId = null; |
162 | 179 | |
163 | 180 | String url = "https://firebasehosting.googleapis.com" + "/v1beta1/sites/" + siteId + "/versions"; |
@@ -164,18 +181,7 @@ | ||
164 | 181 | HttpRequest request = HttpRequest.newBuilder(URI.create(url)) |
165 | 182 | .setHeader("Authorization", "Bearer " + token) |
166 | 183 | .setHeader("Content-Type", "application/json") |
167 | - .POST(HttpRequest.BodyPublishers.ofString(""" | |
168 | - { | |
169 | - "config": { | |
170 | - "headers": [{ | |
171 | - "glob": "**", | |
172 | - "headers": { | |
173 | - "Cache-Control": "max-age=1800" | |
174 | - } | |
175 | - }] | |
176 | - } | |
177 | - } | |
178 | - """, StandardCharsets.UTF_8)) | |
184 | + .POST(HttpRequest.BodyPublishers.ofString("{ \"config\": " + firebaseConfig + " }", StandardCharsets.UTF_8)) | |
179 | 185 | .build(); |
180 | 186 | |
181 | 187 | HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |