Просмотр исходного кода

refactor importing Attachment model lines

Yuki Takei 2 лет назад
Родитель
Сommit
fdaa61b5f0

+ 2 - 3
apps/app/src/migrations/20220613064207-add-attachment-type-to-existing-attachments.js

@@ -1,8 +1,8 @@
 import mongoose from 'mongoose';
 import mongoose from 'mongoose';
 
 
 import { AttachmentType } from '~/server/interfaces/attachment';
 import { AttachmentType } from '~/server/interfaces/attachment';
-import attachmentModel from '~/server/models/attachment';
-import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
+import { Attachment } from '~/server/models';
+import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 const logger = loggerFactory('growi:migrate:add-attachment-type-to-existing-attachments');
 const logger = loggerFactory('growi:migrate:add-attachment-type-to-existing-attachments');
@@ -11,7 +11,6 @@ module.exports = {
   async up(db) {
   async up(db) {
     logger.info('Apply migration');
     logger.info('Apply migration');
     mongoose.connect(getMongoUri(), mongoOptions);
     mongoose.connect(getMongoUri(), mongoOptions);
-    const Attachment = getModelSafely('Attachment') || attachmentModel();
 
 
     // Add attachmentType for wiki page
     // Add attachmentType for wiki page
     // Filter pages where "attachmentType" doesn't exist and "page" is not null
     // Filter pages where "attachmentType" doesn't exist and "page" is not null

+ 8 - 18
apps/app/src/server/crowi/index.js

@@ -18,11 +18,7 @@ import loggerFactory from '~/utils/logger';
 import { projectRoot } from '~/utils/project-dir-utils';
 import { projectRoot } from '~/utils/project-dir-utils';
 
 
 import UserEvent from '../events/user';
 import UserEvent from '../events/user';
-import Activity from '../models/activity';
-import PageRedirect from '../models/page-redirect';
-import ShareLink from '../models/share-link';
-import Tag from '../models/tag';
-import UserGroup from '../models/user-group';
+import { modelsDependsOnCrowi } from '../models';
 import { aclService as aclServiceSingletonInstance } from '../service/acl';
 import { aclService as aclServiceSingletonInstance } from '../service/acl';
 import AppService from '../service/app';
 import AppService from '../service/app';
 import AttachmentService from '../service/attachment';
 import AttachmentService from '../service/attachment';
@@ -39,7 +35,6 @@ import { getMongoUri, mongoOptions } from '../util/mongoose-utils';
 
 
 const logger = loggerFactory('growi:crowi');
 const logger = loggerFactory('growi:crowi');
 const httpErrorHandler = require('../middlewares/http-error-handler');
 const httpErrorHandler = require('../middlewares/http-error-handler');
-const models = require('../models');
 
 
 const sep = path.sep;
 const sep = path.sep;
 
 
@@ -294,20 +289,15 @@ Crowi.prototype.setupSocketIoService = async function() {
 };
 };
 
 
 Crowi.prototype.setupModels = async function() {
 Crowi.prototype.setupModels = async function() {
-  let allModels = {};
+  Object.keys(modelsDependsOnCrowi).forEach((key) => {
+    const factory = modelsDependsOnCrowi[key];
 
 
-  // include models that dependent on crowi
-  allModels = models;
-
-  // include models that independent from crowi
-  allModels.Activity = Activity;
-  allModels.Tag = Tag;
-  allModels.UserGroup = UserGroup;
-  allModels.PageRedirect = PageRedirect;
-  allModels.ShareLink = ShareLink;
+    if (!(factory instanceof Function)) {
+      logger.warn(`modelsDependsOnCrowi['${key}'] is not a function. skipped.`);
+      return;
+    }
 
 
-  Object.keys(allModels).forEach((key) => {
-    return this.model(key, models[key](this));
+    return this.model(key, modelsDependsOnCrowi[key](this));
   });
   });
 
 
 };
 };

+ 10 - 4
apps/app/src/server/models/index.js

@@ -1,18 +1,24 @@
-import Page from '~/server/models/page';
+import Page from './page';
 
 
-module.exports = {
+export const modelsDependsOnCrowi = {
   Page,
   Page,
   PageTagRelation: require('./page-tag-relation'),
   PageTagRelation: require('./page-tag-relation'),
   User: require('./user'),
   User: require('./user'),
   ExternalAccount: require('./external-account'),
   ExternalAccount: require('./external-account'),
   UserGroupRelation: require('./user-group-relation'),
   UserGroupRelation: require('./user-group-relation'),
   Revision: require('./revision'),
   Revision: require('./revision'),
-  Tag: require('./tag'),
   Bookmark: require('./bookmark'),
   Bookmark: require('./bookmark'),
   Comment: require('./comment'),
   Comment: require('./comment'),
-  Attachment: require('./attachment'),
   GlobalNotificationSetting: require('./GlobalNotificationSetting'),
   GlobalNotificationSetting: require('./GlobalNotificationSetting'),
   GlobalNotificationMailSetting: require('./GlobalNotificationSetting/GlobalNotificationMailSetting'),
   GlobalNotificationMailSetting: require('./GlobalNotificationSetting/GlobalNotificationMailSetting'),
   GlobalNotificationSlackSetting: require('./GlobalNotificationSetting/GlobalNotificationSlackSetting'),
   GlobalNotificationSlackSetting: require('./GlobalNotificationSetting/GlobalNotificationSlackSetting'),
   SlackAppIntegration: require('./slack-app-integration'),
   SlackAppIntegration: require('./slack-app-integration'),
 };
 };
+
+// setup models that independent from crowi
+export * from './attachment';
+export * as Activity from './activity';
+export * as PageRedirect from './page-redirect';
+export * as ShareLink from './share-link';
+export * as Tag from './tag';
+export * as UserGroup from './user-group';

+ 2 - 1
apps/app/src/server/models/user.js

@@ -6,6 +6,8 @@ import { i18n } from '^/config/next-i18next.config';
 import { generateGravatarSrc } from '~/utils/gravatar';
 import { generateGravatarSrc } from '~/utils/gravatar';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
+import { Attachment } from './attachment';
+
 
 
 const crypto = require('crypto');
 const crypto = require('crypto');
 
 
@@ -249,7 +251,6 @@ module.exports = function(crowi) {
       return this.image;
       return this.image;
     }
     }
     if (this.imageAttachment != null && this.imageAttachment._id != null) {
     if (this.imageAttachment != null && this.imageAttachment._id != null) {
-      const Attachment = crowi.model('Attachment');
       const imageAttachment = await Attachment.findById(this.imageAttachment);
       const imageAttachment = await Attachment.findById(this.imageAttachment);
       return imageAttachment.filePathProxied;
       return imageAttachment.filePathProxied;
     }
     }

+ 1 - 1
apps/app/src/server/routes/apiv3/attachment.js

@@ -1,5 +1,6 @@
 import { ErrorV3 } from '@growi/core/dist/models';
 import { ErrorV3 } from '@growi/core/dist/models';
 
 
+import { Attachment } from '~/server/models';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
 import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
@@ -24,7 +25,6 @@ module.exports = (crowi) => {
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
   const loginRequired = require('../../middlewares/login-required')(crowi, true);
   const Page = crowi.model('Page');
   const Page = crowi.model('Page');
   const User = crowi.model('User');
   const User = crowi.model('User');
-  const Attachment = crowi.model('Attachment');
 
 
   const validator = {
   const validator = {
     retrieveAttachment: [
     retrieveAttachment: [

+ 1 - 1
apps/app/src/server/routes/apiv3/customize-setting.js

@@ -9,6 +9,7 @@ import multer from 'multer';
 import { GrowiPlugin } from '~/features/growi-plugin/server/models';
 import { GrowiPlugin } from '~/features/growi-plugin/server/models';
 import { SupportedAction } from '~/interfaces/activity';
 import { SupportedAction } from '~/interfaces/activity';
 import { AttachmentType } from '~/server/interfaces/attachment';
 import { AttachmentType } from '~/server/interfaces/attachment';
+import { Attachment } from '~/server/models';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
 import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
@@ -102,7 +103,6 @@ module.exports = (crowi) => {
   const activityEvent = crowi.event('activity');
   const activityEvent = crowi.event('activity');
 
 
   const { customizeService, attachmentService } = crowi;
   const { customizeService, attachmentService } = crowi;
-  const Attachment = crowi.model('Attachment');
   const uploads = multer({ dest: `${crowi.tmpDir}uploads` });
   const uploads = multer({ dest: `${crowi.tmpDir}uploads` });
   const validator = {
   const validator = {
     layout: [
     layout: [

+ 2 - 1
apps/app/src/server/routes/attachment.js

@@ -4,6 +4,8 @@ import { SupportedAction } from '~/interfaces/activity';
 import { AttachmentType } from '~/server/interfaces/attachment';
 import { AttachmentType } from '~/server/interfaces/attachment';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
+import { Attachment } from '../models';
+
 /* eslint-disable no-use-before-define */
 /* eslint-disable no-use-before-define */
 
 
 
 
@@ -134,7 +136,6 @@ const ApiResponse = require('../util/apiResponse');
  */
  */
 
 
 module.exports = function(crowi, app) {
 module.exports = function(crowi, app) {
-  const Attachment = crowi.model('Attachment');
   const Page = crowi.model('Page');
   const Page = crowi.model('Page');
   const User = crowi.model('User');
   const User = crowi.model('User');
   const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
   const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');

+ 1 - 1
apps/app/src/server/routes/ogp.ts

@@ -12,6 +12,7 @@ import { param, validationResult, ValidationError } from 'express-validator';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 import { projectRoot } from '~/utils/project-dir-utils';
 import { projectRoot } from '~/utils/project-dir-utils';
 
 
+import { Attachment } from '../models';
 import { convertStreamToBuffer } from '../util/stream';
 import { convertStreamToBuffer } from '../util/stream';
 
 
 const logger = loggerFactory('growi:routes:ogp');
 const logger = loggerFactory('growi:routes:ogp');
@@ -38,7 +39,6 @@ module.exports = function(crowi) {
 
 
     if (isUserImageAttachment(userImageUrlCached)) {
     if (isUserImageAttachment(userImageUrlCached)) {
       const { fileUploadService } = crowi;
       const { fileUploadService } = crowi;
-      const Attachment = crowi.model('Attachment');
       const attachment = await Attachment.findById(userImageUrlCached);
       const attachment = await Attachment.findById(userImageUrlCached);
       const fileStream = await fileUploadService.findDeliveryFile(attachment);
       const fileStream = await fileUploadService.findDeliveryFile(attachment);
       bufferedUserImage = await convertStreamToBuffer(fileStream);
       bufferedUserImage = await convertStreamToBuffer(fileStream);

+ 1 - 5
apps/app/src/server/service/attachment.js

@@ -1,6 +1,7 @@
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { AttachmentType } from '../interfaces/attachment';
 import { AttachmentType } from '../interfaces/attachment';
+import { Attachment } from '../models';
 
 
 const fs = require('fs');
 const fs = require('fs');
 
 
@@ -27,8 +28,6 @@ class AttachmentService {
       throw new Error(res.errorMessage);
       throw new Error(res.errorMessage);
     }
     }
 
 
-    const Attachment = this.crowi.model('Attachment');
-
     const fileStream = fs.createReadStream(file.path, {
     const fileStream = fs.createReadStream(file.path, {
       flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true,
       flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true,
     });
     });
@@ -69,7 +68,6 @@ class AttachmentService {
   }
   }
 
 
   async removeAttachment(attachmentId) {
   async removeAttachment(attachmentId) {
-    const Attachment = this.crowi.model('Attachment');
     const { fileUploadService } = this.crowi;
     const { fileUploadService } = this.crowi;
     const attachment = await Attachment.findById(attachmentId);
     const attachment = await Attachment.findById(attachmentId);
 
 
@@ -80,8 +78,6 @@ class AttachmentService {
   }
   }
 
 
   async isBrandLogoExist() {
   async isBrandLogoExist() {
-    const Attachment = this.crowi.model('Attachment');
-
     const query = { attachmentType: AttachmentType.BRAND_LOGO };
     const query = { attachmentType: AttachmentType.BRAND_LOGO };
     const count = await Attachment.countDocuments(query);
     const count = await Attachment.countDocuments(query);
 
 

+ 1 - 1
apps/app/src/server/service/file-uploader/aws.ts

@@ -12,7 +12,7 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
 import type { Response } from 'express';
 import type { Response } from 'express';
 import urljoin from 'url-join';
 import urljoin from 'url-join';
 
 
-import type { IAttachmentDocument } from '~/server/models/attachment';
+import type { IAttachmentDocument } from '~/server/models';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { configManager } from '../config-manager';
 import { configManager } from '../config-manager';

+ 1 - 3
apps/app/src/server/service/file-uploader/file-uploader.ts

@@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
 
 
 import type { Response } from 'express';
 import type { Response } from 'express';
 
 
-import type { IAttachmentDocument } from '~/server/models/attachment';
+import { Attachment, type IAttachmentDocument } from '~/server/models';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { configManager } from '../config-manager';
 import { configManager } from '../config-manager';
@@ -110,8 +110,6 @@ export abstract class AbstractFileUploader implements FileUploader {
    * @returns Total file size
    * @returns Total file size
    */
    */
   async getTotalFileSize() {
   async getTotalFileSize() {
-    const Attachment = this.crowi.model('Attachment');
-
     // Get attachment total file size
     // Get attachment total file size
     const res = await Attachment.aggregate().group({
     const res = await Attachment.aggregate().group({
       _id: null,
       _id: null,

+ 1 - 1
apps/app/src/server/service/file-uploader/gcs.ts

@@ -2,7 +2,7 @@ import { Storage } from '@google-cloud/storage';
 import { Response } from 'express';
 import { Response } from 'express';
 import urljoin from 'url-join';
 import urljoin from 'url-join';
 
 
-import { IAttachmentDocument } from '~/server/models/attachment';
+import type { IAttachmentDocument } from '~/server/models';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { configManager } from '../config-manager';
 import { configManager } from '../config-manager';

+ 1 - 1
apps/app/src/server/service/g2g-transfer.ts

@@ -17,6 +17,7 @@ import axios from '~/utils/axios';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 import { TransferKey } from '~/utils/vo/transfer-key';
 import { TransferKey } from '~/utils/vo/transfer-key';
 
 
+import { Attachment } from '../models';
 import { G2GTransferError, G2GTransferErrorCode } from '../models/vo/g2g-transfer-error';
 import { G2GTransferError, G2GTransferErrorCode } from '../models/vo/g2g-transfer-error';
 
 
 const logger = loggerFactory('growi:service:g2g-transfer');
 const logger = loggerFactory('growi:service:g2g-transfer');
@@ -322,7 +323,6 @@ export class G2GTransferPusherService implements Pusher {
     const BATCH_SIZE = 100;
     const BATCH_SIZE = 100;
     const { fileUploadService, socketIoService } = this.crowi;
     const { fileUploadService, socketIoService } = this.crowi;
     const socket = socketIoService.getAdminSocket();
     const socket = socketIoService.getAdminSocket();
-    const Attachment = this.crowi.model('Attachment');
     const filesFromSrcGROWI = await this.listFilesInStorage(tk);
     const filesFromSrcGROWI = await this.listFilesInStorage(tk);
 
 
     /**
     /**

+ 1 - 1
apps/app/src/server/service/page.ts

@@ -31,6 +31,7 @@ import loggerFactory from '~/utils/logger';
 import { prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config';
 import { prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config';
 
 
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
+import { Attachment } from '../models';
 import { PathAlreadyExistsError } from '../models/errors';
 import { PathAlreadyExistsError } from '../models/errors';
 import { IOptionsForCreate, IOptionsForUpdate } from '../models/interfaces/page-operation';
 import { IOptionsForCreate, IOptionsForUpdate } from '../models/interfaces/page-operation';
 import PageOperation, { PageOperationDocument } from '../models/page-operation';
 import PageOperation, { PageOperationDocument } from '../models/page-operation';
@@ -1692,7 +1693,6 @@ class PageService {
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
     const PageTagRelation = this.crowi.model('PageTagRelation');
     const PageTagRelation = this.crowi.model('PageTagRelation');
     const Revision = this.crowi.model('Revision');
     const Revision = this.crowi.model('Revision');
-    const Attachment = this.crowi.model('Attachment');
     const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
     const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
 
 
     const { attachmentService } = this.crowi;
     const { attachmentService } = this.crowi;

+ 1 - 0
packages/remark-attachment-refs/package.json

@@ -48,6 +48,7 @@
     "axios": "^0.24.0",
     "axios": "^0.24.0",
     "bunyan": "^1.8.15",
     "bunyan": "^1.8.15",
     "hast-util-select": "^5.0.5",
     "hast-util-select": "^5.0.5",
+    "mongoose": "^6.11.3",
     "swr": "^2.0.3",
     "swr": "^2.0.3",
     "universal-bunyan": "^0.9.2"
     "universal-bunyan": "^0.9.2"
   },
   },

+ 7 - 4
packages/remark-attachment-refs/src/server/routes/refs.ts

@@ -1,4 +1,6 @@
+import type { IAttachment } from '@growi/core';
 import { OptionParser } from '@growi/core/dist/remark-plugins';
 import { OptionParser } from '@growi/core/dist/remark-plugins';
+import { model } from 'mongoose';
 
 
 import loggerFactory from '../../utils/logger';
 import loggerFactory from '../../utils/logger';
 
 
@@ -24,7 +26,6 @@ export const routesFactory = (crowi): any => {
 
 
   const User = crowi.model('User');
   const User = crowi.model('User');
   const Page = crowi.model('Page');
   const Page = crowi.model('Page');
-  const Attachment = crowi.model('Attachment');
 
 
   const { PageQueryBuilder } = Page;
   const { PageQueryBuilder } = Page;
 
 
@@ -100,6 +101,7 @@ export const routesFactory = (crowi): any => {
       orConditions.push({ _id: ObjectId(fileNameOrId) });
       orConditions.push({ _id: ObjectId(fileNameOrId) });
     }
     }
 
 
+    const Attachment = model<IAttachment>('Attachment');
     const attachment = await Attachment
     const attachment = await Attachment
       .findOne({
       .findOne({
         page: page._id,
         page: page._id,
@@ -189,15 +191,16 @@ export const routesFactory = (crowi): any => {
     logger.debug('retrieve attachments for pages:', pageIds);
     logger.debug('retrieve attachments for pages:', pageIds);
 
 
     // create query to find
     // create query to find
+    const Attachment = model<IAttachment>('Attachment');
     let query = Attachment
     let query = Attachment
       .find({
       .find({
         page: { $in: pageIds },
         page: { $in: pageIds },
       });
       });
     // add regex condition
     // add regex condition
     if (regex != null) {
     if (regex != null) {
-      query = query.and({
-        originalName: { $regex: regex },
-      });
+      query = query.and([
+        { originalName: { $regex: regex } },
+      ]);
     }
     }
 
 
     const attachments = await query
     const attachments = await query