-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feature: add upload image from URL support
- Loading branch information
1 parent
7975f73
commit 0d87342
Showing
6 changed files
with
114 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isUrl = (url: string): boolean => (url.startsWith('http://') || url.startsWith('https://')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import PicGo from '../core/PicGo' | ||
import request from 'request' | ||
import path from 'path' | ||
import { IPathTransformedImgInfo } from './interfaces' | ||
import fs from 'fs' | ||
|
||
export const getURLFile = async (ctx: PicGo, url: string): Promise<IPathTransformedImgInfo> => { | ||
const requestOptions = { | ||
method: 'GET', | ||
url, | ||
encoding: null | ||
} | ||
let isImage = false | ||
let extname = '' | ||
let timeoutId | ||
// tslint:disable-next-line: typedef | ||
const requestPromise = new Promise<IPathTransformedImgInfo>(async (resolve): Promise<void> => { | ||
try { | ||
const res = await ctx.Request.request(requestOptions) | ||
.on('response', (response: request.Response): void => { | ||
const contentType = response.headers['content-type'] | ||
if (contentType.includes('image')) { | ||
isImage = true | ||
extname = `.${contentType.split('image/')[1]}` | ||
} | ||
}) | ||
clearTimeout(timeoutId) | ||
if (isImage) { | ||
fs.writeFileSync('./logo.png', res) | ||
resolve({ | ||
buffer: res, | ||
fileName: path.basename(requestOptions.url.split('?')[0]), | ||
extname, | ||
success: true | ||
}) | ||
} else { | ||
resolve({ | ||
success: false, | ||
reason: `${url} is not image` | ||
}) | ||
} | ||
} catch { | ||
clearTimeout(timeoutId) | ||
resolve({ | ||
success: false, | ||
reason: `request ${url} error` | ||
}) | ||
} | ||
}) | ||
// tslint:disable-next-line: typedef | ||
const timeoutPromise = new Promise<IPathTransformedImgInfo>((resolve): void => { | ||
timeoutId = setTimeout(() => { | ||
resolve({ | ||
success: false, | ||
reason: `request ${url} timeout` | ||
}) | ||
}, 10000) | ||
}) | ||
return Promise.race([requestPromise, timeoutPromise]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters