Add migration

This commit is contained in:
syuilo 2019-02-28 15:44:23 +09:00
parent b5c235f837
commit d6db5e46e8
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
9 changed files with 49 additions and 15 deletions

View file

@ -4,15 +4,49 @@ import { pack as packFolder } from './drive-folder';
import { pack as packUser } from './user';
import monkDb, { nativeDbConn, dbLogger } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import config from '../config';
import uuid = require('uuid');
const DriveFile = monkDb.get<IDriveFile>('driveFiles.files');
DriveFile.createIndex('md5');
DriveFile.createIndex('metadata.uri');
DriveFile.createIndex('metadata.url');
DriveFile.createIndex('metadata.webpublicUrl');
DriveFile.createIndex('metadata.thumbnailUrl');
DriveFile.createIndex('metadata.userId');
DriveFile.createIndex('metadata.folderId');
DriveFile.createIndex('metadata._user.host');
export default DriveFile;
// 後方互換性のため
DriveFile.findOne({
$or: [{
'metadata.url': { $exists: false }
}, {
'metadata.url': null
}]
}).then(x => {
if (x != null) {
DriveFile.find({
$or: [{
'metadata.url': { $exists: false }
}, {
'metadata.url': null
}]
}, { fields: { _id: true } }).then(xs => {
for (const x of xs) {
DriveFile.update({ _id: x._id }, {
$set: {
'metadata.url': `${config.driveUrl}/${uuid.v4()}`,
'metadata.webpublicUrl': `${config.driveUrl}/${uuid.v4()}?web`,
'metadata.thumbnailUrl': `${config.driveUrl}/${uuid.v4()}?thumbnail`,
}
});
}
});
}
});
export const DriveFileChunk = monkDb.get('driveFiles.chunks');
export const getDriveFileBucket = async (): Promise<mongo.GridFSBucket> => {

View file

@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as mongo from 'mongodb';
import { queueLogger } from '../logger';
import addFile from '../../services/drive/add-file';
import { addDriveFile } from '../../services/drive/add-file';
import User from '../../models/user';
import dateFormat = require('dateformat');
import Blocking from '../../models/blocking';
@ -81,7 +81,7 @@ export async function exportBlocking(job: bq.Job, done: any): Promise<void> {
logger.succ(`Exported to: ${path}`);
const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
const driveFile = await addFile(user, path, fileName);
const driveFile = await addDriveFile(user, path, fileName);
logger.succ(`Exported to: ${driveFile._id}`);
cleanup();

View file

@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as mongo from 'mongodb';
import { queueLogger } from '../logger';
import addFile from '../../services/drive/add-file';
import { addDriveFile } from '../../services/drive/add-file';
import User from '../../models/user';
import dateFormat = require('dateformat');
import Following from '../../models/following';
@ -81,7 +81,7 @@ export async function exportFollowing(job: bq.Job, done: any): Promise<void> {
logger.succ(`Exported to: ${path}`);
const fileName = 'following-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
const driveFile = await addFile(user, path, fileName);
const driveFile = await addDriveFile(user, path, fileName);
logger.succ(`Exported to: ${driveFile._id}`);
cleanup();

View file

@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as mongo from 'mongodb';
import { queueLogger } from '../logger';
import addFile from '../../services/drive/add-file';
import { addDriveFile } from '../../services/drive/add-file';
import User from '../../models/user';
import dateFormat = require('dateformat');
import Mute from '../../models/mute';
@ -81,7 +81,7 @@ export async function exportMute(job: bq.Job, done: any): Promise<void> {
logger.succ(`Exported to: ${path}`);
const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
const driveFile = await addFile(user, path, fileName);
const driveFile = await addDriveFile(user, path, fileName);
logger.succ(`Exported to: ${driveFile._id}`);
cleanup();

View file

@ -5,7 +5,7 @@ import * as mongo from 'mongodb';
import { queueLogger } from '../logger';
import Note, { INote } from '../../models/note';
import addFile from '../../services/drive/add-file';
import { addDriveFile } from '../../services/drive/add-file';
import User from '../../models/user';
import dateFormat = require('dateformat');
@ -101,7 +101,7 @@ export async function exportNotes(job: bq.Job, done: any): Promise<void> {
logger.succ(`Exported to: ${path}`);
const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json';
const driveFile = await addFile(user, path, fileName);
const driveFile = await addDriveFile(user, path, fileName);
logger.succ(`Exported to: ${driveFile._id}`);
cleanup();

View file

@ -2,7 +2,7 @@ import * as ms from 'ms';
import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
import { validateFileName, pack } from '../../../../../models/drive-file';
import create from '../../../../../services/drive/add-file';
import { addDriveFile } from '../../../../../services/drive/add-file';
import define from '../../../define';
import { apiLogger } from '../../../logger';
import { ApiError } from '../../../error';
@ -87,7 +87,7 @@ export default define(meta, async (ps, user, app, file, cleanup) => {
try {
// Create file
const driveFile = await create(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive);
const driveFile = await addDriveFile(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive);
return pack(driveFile, { self: true });
} catch (e) {
apiLogger.error(e);

View file

@ -276,7 +276,7 @@ async function deleteOldFile(user: IRemoteUser) {
* @param sensitive Mark file as sensitive
* @return Created drive file
*/
export default async function(
export async function addDriveFile(
user: IUser,
path: string,
name: string = null,

View file

@ -48,8 +48,8 @@ export default async function(file: IDriveFile, isExpired = false) {
set.metadata.withoutChunks = true;
set.metadata.isRemote = true;
set.metadata.url = file.metadata.uri;
set.metadata.thumbnailUrl = undefined;
set.metadata.webpublicUrl = undefined;
set.metadata.thumbnailUrl = file.metadata.uri;
set.metadata.webpublicUrl = file.metadata.uri;
}
await DriveFile.update({ _id: file._id }, {

View file

@ -4,7 +4,7 @@ import * as tmp from 'tmp';
import * as request from 'request';
import { IDriveFile, validateFileName } from '../../models/drive-file';
import create from './add-file';
import { addDriveFile } from './add-file';
import config from '../../config';
import { IUser } from '../../models/user';
import * as mongodb from 'mongodb';
@ -83,7 +83,7 @@ export default async (
let error;
try {
driveFile = await create(user, path, name, null, folderId, force, link, url, uri, sensitive);
driveFile = await addDriveFile(user, path, name, null, folderId, force, link, url, uri, sensitive);
logger.succ(`Got: ${driveFile._id}`);
} catch (e) {
error = e;