soiz1 commited on
Commit
049b41a
·
verified ·
1 Parent(s): 8d8633d

Update src/addons/addons/save-to-google/userscript.js

Browse files
src/addons/addons/save-to-google/userscript.js CHANGED
@@ -75,7 +75,8 @@ export default async ({ addon, console, msg }) => {
75
  form.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" }));
76
  form.append("file", blob);
77
 
78
- const response = await fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", {
 
79
  method: "POST",
80
  headers: {
81
  Authorization: `Bearer ${accessToken}`,
@@ -83,13 +84,32 @@ export default async ({ addon, console, msg }) => {
83
  body: form,
84
  });
85
 
86
- if (!response.ok) {
87
- throw new Error(await response.text());
88
  }
89
 
90
- const data = await response.json();
91
- console.log("ファイルがGoogleドライブに保存されました:", data);
92
- showAlert(addon, "success", "Googleドライブに保存しました");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  if (onSuccess) onSuccess();
94
  } catch (error) {
95
  console.error("保存エラー:", error);
 
75
  form.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" }));
76
  form.append("file", blob);
77
 
78
+ // ファイルをアップロード
79
+ const uploadResponse = await fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", {
80
  method: "POST",
81
  headers: {
82
  Authorization: `Bearer ${accessToken}`,
 
84
  body: form,
85
  });
86
 
87
+ if (!uploadResponse.ok) {
88
+ throw new Error(await uploadResponse.text());
89
  }
90
 
91
+ const fileData = await uploadResponse.json();
92
+ console.log("ファイルがGoogleドライブに保存されました:", fileData);
93
+
94
+ // 権限を設定 (リンクを知っている全員が閲覧可能に)
95
+ const permissionResponse = await fetch(`https://www.googleapis.com/drive/v3/files/${fileData.id}/permissions`, {
96
+ method: "POST",
97
+ headers: {
98
+ Authorization: `Bearer ${accessToken}`,
99
+ "Content-Type": "application/json",
100
+ },
101
+ body: JSON.stringify({
102
+ role: "reader",
103
+ type: "anyone",
104
+ }),
105
+ });
106
+
107
+ if (!permissionResponse.ok) {
108
+ throw new Error(await permissionResponse.text());
109
+ }
110
+
111
+ console.log("ファイルの権限が設定されました");
112
+ showAlert(addon, "success", "Googleドライブに保存しました (リンクを知っている全員が閲覧可能)");
113
  if (onSuccess) onSuccess();
114
  } catch (error) {
115
  console.error("保存エラー:", error);