Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot name file #14

Closed
maka-io opened this issue Oct 5, 2017 · 5 comments
Closed

Cannot name file #14

maka-io opened this issue Oct 5, 2017 · 5 comments
Labels

Comments

@maka-io
Copy link

maka-io commented Oct 5, 2017

I don't see a way to name the files as they are uploaded, they just get a name of "upload_" is there a way to change that, or append an extension?

@jaydenseric
Copy link
Owner

jaydenseric commented Oct 5, 2017

The original filenames are available, you can rename the uploads to whatever you like when moving them to permanent storage.

@maka-io
Copy link
Author

maka-io commented Oct 5, 2017

Ok, so that's something I'd have to do manually? There isn't a setting?

@jaydenseric
Copy link
Owner

jaydenseric commented Oct 5, 2017

Yes, it is something you need to do manually.

You might find this earlier discussion interesting, although these days I do things a bit diffirently and permanently store files in MongoDB GridFS.

In the next several weeks, once our platform launches and I get back from a trip to San Fransisco for GraphQL Summit, I hope to work on providing file streams instead of metadata to resolvers (#13).

Hope that helps 🙂

@maka-io
Copy link
Author

maka-io commented Oct 5, 2017

That does, and thank you!

@vladinator1000
Copy link

vladinator1000 commented Oct 6, 2017

Yeah, you'd have to rename it after upload manually. I ended up not storing stuff in my database, just using Amazon S3 with s3fs and deleting the temp upload folder every now and then:

import S3FS from 's3fs'; // Amazon web services file system
import config from '../../config';

const amazonFileSystem = new S3FS('yourBucket',	{
	region: config.amazonS3.region,
	accessKeyId: config.amazonS3.id,
	secretAccessKey: config.amazonS3.secret,
});

export default amazonFileSystem;
import fs from 'fs';
import readChunk from 'read-chunk';
import fileType from 'file-type';
import uuid from 'uuid';

async function uploadToAmazon(path, name = uuid.v4()) {
	try {
		// Get File Type
		// Only needs first 4100 bytes
		const fileTypeBuffer = await readChunk(path, 0, 4100);
		const type = fileType(fileTypeBuffer);

		if (!type) {
			throw new Error('No file type.');
		}

		// RegExp only allow image mime type
		if (!/^image/.test(type.mime)) {
			throw new Error('Wrong file type.');
		}

		// Generate thumbnails and compress

		const newFileName = `${name}.${type.ext}`;
		const stream = fs.createReadStream(path);

		// Upload file to Amazon S3
		amazonFileSystem.writeFile(
			newFileName,
			stream,
			{ ContentType: type.mime },
		);

		return `https://s3.${config.amazonS3.region}.amazonaws.com/yourBucket/${newFileName}`;
	} catch (e) {
		throw new Error(e);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants