Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

grapql file upload

// cal like this in your resolvers
const { filename, createReadStream } = await photo.promise

// helper file upload
const { resolve } = require('path')

const uploadFile = ({ filename, stream }) => {
	let linux = '/tmp'
	let window = process.env.TEMP || ''

	if (process.platform !== 'win32') {
		stream.pipe(createWriteStream(resolve(process.cwd(), `${linux}/${filename}`)))
	} else {
		stream.pipe(createWriteStream(resolve(process.cwd(), `${window}/${filename}`)))
	}
}

const validateFile = ({ filename, stream }) => {
	const extFile = filename.replace('.', '')
	const extPattern = /(jpg|jpeg|png|gif)/gi.test(extFile)

	if (!extPattern) throw new TypeError('Image format is not valid')
	else return uploadFile({ filename, stream })
}

export const fileUpload = ({ filename, stream }) =>
	filename ? validateFile({ filename, stream }) : new Error('Image is required')
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to pass function arguments without invoke the function 
Javascript :: get ordinal number 
Javascript :: mongoose populate not working 
Javascript :: Image and other field save using Multiparty 
Javascript :: react google login button size increase 
Javascript :: bookshelf log query 
Javascript :: random number in range javascript 
Javascript :: convert number to words javascript lakh 
Javascript :: dlowlad the json fiel in react js pretty formate 
Javascript :: angular resolver for cookie 
Javascript :: javascript refresh function every 5 seconds 
Javascript :: Javascript index,length,push,pop,shift,unshift 
Javascript :: react movies 
Javascript :: show hide pseudo element jquery 
Javascript :: concatenation mathematics notation 
Javascript :: find NaN in js return index 
Javascript :: how to retrieve get parameters from javascript 
Javascript :: invite tracker node.js v13 
Javascript :: a to z in js using while 
Javascript :: select not input elements text JS 
Javascript :: how to check if we reach end of div react hooks 
Javascript :: moment js get dd/mm/yyyy 
Javascript :: Das komplette JavaScript in den Footer laden 
Javascript :: flow parsing package.json and showing error 
Javascript :: jquery swap table rows 
Javascript :: Node-Red: Bit Switch 
Javascript :: discord.js get message content 
Javascript :: arrow function with computed property vue 
Javascript :: rnpm react-images-uploading 
Javascript :: how can i add + buttons for expand and - button for collapse in react 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =