diff options
| author | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-03-27 16:51:12 +0900 |
|---|---|---|
| committer | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-03-27 23:51:21 +0900 |
| commit | 68ce6d574882c1badbb4a3d2772451534014dd01 (patch) | |
| tree | 3b468556c25dd5b63e3774aca1869b71dd9b1919 /src/api/common | |
| parent | Merge pull request #1316 from akihikodaki/host (diff) | |
| download | sharkey-68ce6d574882c1badbb4a3d2772451534014dd01.tar.gz sharkey-68ce6d574882c1badbb4a3d2772451534014dd01.tar.bz2 sharkey-68ce6d574882c1badbb4a3d2772451534014dd01.zip | |
Implement remote account resolution
Diffstat (limited to 'src/api/common')
| -rw-r--r-- | src/api/common/drive/add-file.ts (renamed from src/api/common/add-file-to-drive.ts) | 15 | ||||
| -rw-r--r-- | src/api/common/drive/upload_from_url.ts | 46 | ||||
| -rw-r--r-- | src/api/common/get-host-lower.ts | 5 | ||||
| -rw-r--r-- | src/api/common/text/elements/mention.ts | 7 |
4 files changed, 64 insertions, 9 deletions
diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/drive/add-file.ts index 1ee455c092..c4f2f212ac 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/drive/add-file.ts @@ -10,17 +10,18 @@ import * as debug from 'debug'; import fileType = require('file-type'); import prominence = require('prominence'); -import DriveFile, { getGridFSBucket } from '../models/drive-file'; -import DriveFolder from '../models/drive-folder'; -import { pack } from '../models/drive-file'; -import event, { publishDriveStream } from '../event'; -import config from '../../conf'; +import DriveFile, { getGridFSBucket } from '../../models/drive-file'; +import DriveFolder from '../../models/drive-folder'; +import { pack } from '../../models/drive-file'; +import event, { publishDriveStream } from '../../event'; +import getAcct from '../../../common/user/get-acct'; +import config from '../../../conf'; const gm = _gm.subClass({ imageMagick: true }); -const log = debug('misskey:register-drive-file'); +const log = debug('misskey:drive:add-file'); const tmpFile = (): Promise<string> => new Promise((resolve, reject) => { tmp.file((e, path) => { @@ -46,7 +47,7 @@ const addFile = async ( folderId: mongodb.ObjectID = null, force: boolean = false ) => { - log(`registering ${name} (user: ${user.username}, path: ${path})`); + log(`registering ${name} (user: ${getAcct(user)}, path: ${path})`); // Calculate hash, get content type and get file size const [hash, [mime, ext], size] = await Promise.all([ diff --git a/src/api/common/drive/upload_from_url.ts b/src/api/common/drive/upload_from_url.ts new file mode 100644 index 0000000000..5dd9695936 --- /dev/null +++ b/src/api/common/drive/upload_from_url.ts @@ -0,0 +1,46 @@ +import * as URL from 'url'; +import { IDriveFile, validateFileName } from '../../models/drive-file'; +import create from './add-file'; +import * as debug from 'debug'; +import * as tmp from 'tmp'; +import * as fs from 'fs'; +import * as request from 'request'; + +const log = debug('misskey:common:drive:upload_from_url'); + +export default async (url, user, folderId = null): Promise<IDriveFile> => { + let name = URL.parse(url).pathname.split('/').pop(); + if (!validateFileName(name)) { + name = null; + } + + // Create temp file + const path = await new Promise((res: (string) => void, rej) => { + tmp.file((e, path) => { + if (e) return rej(e); + res(path); + }); + }); + + // write content at URL to temp file + await new Promise((res, rej) => { + const writable = fs.createWriteStream(path); + request(url) + .on('error', rej) + .on('end', () => { + writable.close(); + res(path); + }) + .pipe(writable) + .on('error', rej); + }); + + const driveFile = await create(user, path, name, null, folderId); + + // clean-up + fs.unlink(path, (e) => { + if (e) log(e.stack); + }); + + return driveFile; +}; diff --git a/src/api/common/get-host-lower.ts b/src/api/common/get-host-lower.ts new file mode 100644 index 0000000000..fc4b30439e --- /dev/null +++ b/src/api/common/get-host-lower.ts @@ -0,0 +1,5 @@ +import { toUnicode } from 'punycode'; + +export default host => { + return toUnicode(host).replace(/[A-Z]+/, match => match.toLowerCase()); +}; diff --git a/src/api/common/text/elements/mention.ts b/src/api/common/text/elements/mention.ts index e0fac4dd76..2025dfdaad 100644 --- a/src/api/common/text/elements/mention.ts +++ b/src/api/common/text/elements/mention.ts @@ -1,14 +1,17 @@ /** * Mention */ +import parseAcct from '../../../../common/user/parse-acct'; module.exports = text => { - const match = text.match(/^@[a-zA-Z0-9\-]+/); + const match = text.match(/^(?:@[a-zA-Z0-9\-]+){1,2}/); if (!match) return null; const mention = match[0]; + const { username, host } = parseAcct(mention.substr(1)); return { type: 'mention', content: mention, - username: mention.substr(1) + username, + host }; }; |