gcs.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { Storage } from '@google-cloud/storage';
  2. import urljoin from 'url-join';
  3. import type Crowi from '~/server/crowi';
  4. import { ResponseMode, type RespondOptions } from '~/server/interfaces/attachment';
  5. import type { IAttachmentDocument } from '~/server/models';
  6. import loggerFactory from '~/utils/logger';
  7. import { configManager } from '../config-manager';
  8. import {
  9. AbstractFileUploader, type TemporaryUrl, type SaveFileParam,
  10. } from './file-uploader';
  11. import { ContentHeaders } from './utils';
  12. const logger = loggerFactory('growi:service:fileUploaderGcs');
  13. function getGcsBucket() {
  14. return configManager.getConfig('crowi', 'gcs:bucket');
  15. }
  16. let storage: Storage;
  17. function getGcsInstance() {
  18. if (storage == null) {
  19. const keyFilename = configManager.getConfig('crowi', 'gcs:apiKeyJsonPath');
  20. // see https://googleapis.dev/nodejs/storage/latest/Storage.html
  21. storage = keyFilename != null
  22. ? new Storage({ keyFilename }) // Create a client with explicit credentials
  23. : new Storage(); // Create a client that uses Application Default Credentials
  24. }
  25. return storage;
  26. }
  27. function getFilePathOnStorage(attachment) {
  28. const namespace = configManager.getConfig('crowi', 'gcs:uploadNamespace');
  29. // const namespace = null;
  30. const dirName = (attachment.page != null)
  31. ? 'attachment'
  32. : 'user';
  33. const filePath = urljoin(namespace || '', dirName, attachment.fileName);
  34. return filePath;
  35. }
  36. /**
  37. * check file existence
  38. * @param {File} file https://googleapis.dev/nodejs/storage/latest/File.html
  39. */
  40. async function isFileExists(file) {
  41. // check file exists
  42. const res = await file.exists();
  43. return res[0];
  44. }
  45. // TODO: rewrite this module to be a type-safe implementation
  46. class GcsFileUploader extends AbstractFileUploader {
  47. /**
  48. * @inheritdoc
  49. */
  50. override isValidUploadSettings(): boolean {
  51. throw new Error('Method not implemented.');
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. override listFiles() {
  57. throw new Error('Method not implemented.');
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. override saveFile(param: SaveFileParam) {
  63. throw new Error('Method not implemented.');
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. override deleteFiles() {
  69. throw new Error('Method not implemented.');
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. override determineResponseMode() {
  75. return configManager.getConfig('crowi', 'gcs:referenceFileWithRelayMode')
  76. ? ResponseMode.RELAY
  77. : ResponseMode.REDIRECT;
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. override respond(): void {
  83. throw new Error('GcsFileUploader does not support ResponseMode.DELEGATE.');
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. override async findDeliveryFile(attachment: IAttachmentDocument): Promise<NodeJS.ReadableStream> {
  89. if (!this.getIsReadable()) {
  90. throw new Error('GCS is not configured.');
  91. }
  92. const gcs = getGcsInstance();
  93. const myBucket = gcs.bucket(getGcsBucket());
  94. const filePath = getFilePathOnStorage(attachment);
  95. const file = myBucket.file(filePath);
  96. // check file exists
  97. const isExists = await isFileExists(file);
  98. if (!isExists) {
  99. throw new Error(`Any object that relate to the Attachment (${filePath}) does not exist in GCS`);
  100. }
  101. try {
  102. return file.createReadStream();
  103. }
  104. catch (err) {
  105. logger.error(err);
  106. throw new Error(`Coudn't get file from AWS for the Attachment (${attachment._id.toString()})`);
  107. }
  108. }
  109. /**
  110. * @inheritDoc
  111. */
  112. override async generateTemporaryUrl(attachment: IAttachmentDocument, opts?: RespondOptions): Promise<TemporaryUrl> {
  113. if (!this.getIsUploadable()) {
  114. throw new Error('GCS is not configured.');
  115. }
  116. const gcs = getGcsInstance();
  117. const myBucket = gcs.bucket(getGcsBucket());
  118. const filePath = getFilePathOnStorage(attachment);
  119. const file = myBucket.file(filePath);
  120. const lifetimeSecForTemporaryUrl = configManager.getConfig('crowi', 'gcs:lifetimeSecForTemporaryUrl');
  121. // issue signed url (default: expires 120 seconds)
  122. // https://cloud.google.com/storage/docs/access-control/signed-urls
  123. const isDownload = opts?.download ?? false;
  124. const contentHeaders = new ContentHeaders(attachment, { inline: !isDownload });
  125. const [signedUrl] = await file.getSignedUrl({
  126. action: 'read',
  127. expires: Date.now() + lifetimeSecForTemporaryUrl * 1000,
  128. responseType: contentHeaders.contentType?.value.toString(),
  129. responseDisposition: contentHeaders.contentDisposition?.value.toString(),
  130. });
  131. return {
  132. url: signedUrl,
  133. lifetimeSec: lifetimeSecForTemporaryUrl,
  134. };
  135. }
  136. }
  137. module.exports = function(crowi: Crowi) {
  138. const lib = new GcsFileUploader(crowi);
  139. lib.isValidUploadSettings = function() {
  140. return configManager.getConfig('crowi', 'gcs:apiKeyJsonPath') != null
  141. && configManager.getConfig('crowi', 'gcs:bucket') != null;
  142. };
  143. (lib as any).deleteFile = function(attachment) {
  144. const filePath = getFilePathOnStorage(attachment);
  145. return (lib as any).deleteFilesByFilePaths([filePath]);
  146. };
  147. (lib as any).deleteFiles = function(attachments) {
  148. const filePaths = attachments.map((attachment) => {
  149. return getFilePathOnStorage(attachment);
  150. });
  151. return (lib as any).deleteFilesByFilePaths(filePaths);
  152. };
  153. (lib as any).deleteFilesByFilePaths = function(filePaths) {
  154. if (!lib.getIsUploadable()) {
  155. throw new Error('GCS is not configured.');
  156. }
  157. const gcs = getGcsInstance();
  158. const myBucket = gcs.bucket(getGcsBucket());
  159. const files = filePaths.map((filePath) => {
  160. return myBucket.file(filePath);
  161. });
  162. files.forEach((file) => {
  163. file.delete({ ignoreNotFound: true });
  164. });
  165. };
  166. (lib as any).uploadAttachment = function(fileStream, attachment) {
  167. if (!lib.getIsUploadable()) {
  168. throw new Error('GCS is not configured.');
  169. }
  170. logger.debug(`File uploading: fileName=${attachment.fileName}`);
  171. const gcs = getGcsInstance();
  172. const myBucket = gcs.bucket(getGcsBucket());
  173. const filePath = getFilePathOnStorage(attachment);
  174. const contentHeaders = new ContentHeaders(attachment);
  175. return myBucket.upload(fileStream.path, {
  176. destination: filePath,
  177. // put type and the file name for reference information when uploading
  178. contentType: contentHeaders.contentType?.value.toString(),
  179. });
  180. };
  181. lib.saveFile = async function({ filePath, contentType, data }) {
  182. const gcs = getGcsInstance();
  183. const myBucket = gcs.bucket(getGcsBucket());
  184. return myBucket.file(filePath).save(data, { resumable: false });
  185. };
  186. /**
  187. * check the file size limit
  188. *
  189. * In detail, the followings are checked.
  190. * - per-file size limit (specified by MAX_FILE_SIZE)
  191. */
  192. (lib as any).checkLimit = async function(uploadFileSize) {
  193. const maxFileSize = configManager.getConfig('crowi', 'app:maxFileSize');
  194. const gcsTotalLimit = configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
  195. return lib.doCheckLimit(uploadFileSize, maxFileSize, gcsTotalLimit);
  196. };
  197. /**
  198. * List files in storage
  199. */
  200. (lib as any).listFiles = async function() {
  201. if (!lib.getIsReadable()) {
  202. throw new Error('GCS is not configured.');
  203. }
  204. const gcs = getGcsInstance();
  205. const bucket = gcs.bucket(getGcsBucket());
  206. const [files] = await bucket.getFiles({
  207. prefix: configManager.getConfig('crowi', 'gcs:uploadNamespace'),
  208. });
  209. return files.map(({ name, metadata: { size } }) => {
  210. return { name, size };
  211. });
  212. };
  213. return lib;
  214. };