Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #168 from Galooshi/uploader-tweaks
Browse files Browse the repository at this point in the history
Uploader tweaks
  • Loading branch information
lencioni authored Dec 14, 2016
2 parents c0402df + 4449466 commit b7cd7ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "happo",
"version": "3.0.0",
"version": "3.0.1-beta.1",
"description": "Command-line tool to visually diff JavaScript components",
"bin": {
"happo": "./bin/happo"
Expand Down
30 changes: 17 additions & 13 deletions src/server/S3Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ const {
module.exports = class S3Uploader {
constructor({ debug } = {}) {
this.debug = debug;
this.debugLog(`Initializing S3 configuration for ${S3_ACCESS_KEY_ID}\n`);
this.debugLog(`Initializing S3 configuration for ${S3_ACCESS_KEY_ID}`);

AWS.config = new AWS.Config({
this.s3 = new AWS.S3({
accessKeyId: S3_ACCESS_KEY_ID,
secretAccessKey: S3_SECRET_ACCESS_KEY,
region: S3_REGION || 'us-west-2',
region: S3_REGION || 'us-east-1',
logger: debug && process.stderr,
});

this.s3 = new AWS.S3();

this.directory = [
S3_BUCKET_PATH,
crypto.randomBytes(16).toString('hex'),
].filter(Boolean).join('/');

this.debugLog(`Setting S3 directory to ${this.directory}\n`);
this.debugLog(`Setting S3 directory to ${this.directory}`);
}

debugLog(message) {
Expand All @@ -40,7 +38,7 @@ module.exports = class S3Uploader {

// We print to stderr to avoid messing with the end result (the link to the
// uploaded HTML file)
process.stderr.write(message);
process.stderr.write(`${message}\n`);
}

/**
Expand All @@ -50,17 +48,23 @@ module.exports = class S3Uploader {
*/
prepare() {
return new Promise((resolve, reject) => {
this.debugLog(`Checking for bucket ${Bucket}\n`);
this.debugLog(`Checking for bucket ${Bucket}`);

this.s3.headBucket({ Bucket }, (headErr) => {
if (headErr) {
this.debugLog(`Bucket not found, creating new bucket ${Bucket}\n`);
if (headErr.statusCode === 403) {
this.debugLog(`Access denied, assuming bucket exists ${Bucket}`);
resolve();
return;
}

this.debugLog(`Bucket not found, creating new bucket ${Bucket}`);
this.s3.createBucket({ Bucket }, (createErr) => {
if (createErr) {
this.debugLog(`Bucket creation failed ${Bucket}\n`);
this.debugLog(`Bucket creation failed ${Bucket}`);
reject(createErr);
} else {
this.debugLog(`Bucket creation successful ${Bucket}\n`);
this.debugLog(`Bucket creation successful ${Bucket}`);
resolve();
}
});
Expand Down Expand Up @@ -94,10 +98,10 @@ module.exports = class S3Uploader {
Key: `${this.directory}/${fileName}`,
};

this.debugLog('Attempting upload\n');
this.debugLog('Attempting upload');
this.s3.upload(uploadParams, (err, { Location }) => {
if (err) {
this.debugLog('Upload failed\n');
this.debugLog('Upload failed');
reject(err);
} else {
resolve(Location);
Expand Down

0 comments on commit b7cd7ff

Please sign in to comment.