Skip to content

Commit 47ef965

Browse files
committed
Fixes for attachments
1 parent 7572017 commit 47ef965

1 file changed

Lines changed: 46 additions & 35 deletions

File tree

src/utils.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,53 @@ export const migrateAttachments = async (body: string, githubRepoId: number | un
4747
for (const match of matches) {
4848
const name = match[1];
4949
const url = match[2];
50-
51-
const basename = path.basename(url);
52-
const extension = path.extname(url);
53-
const mimeType = mime.lookup(basename);
54-
const attachmentBuffer = await gitlabHelper.getAttachment(url);
55-
56-
// Generate new random file name for S3 bucket
57-
const id = crypto.randomBytes(16).toString('hex');
58-
const newFileName = id + extension;
59-
const relativePath = githubRepoId ? `${githubRepoId}/${newFileName}` : newFileName;
60-
61-
// Doesn't seem like it is easy to upload an issue to github, so upload to S3
62-
//https://stackoverflow.com/questions/41581151/how-to-upload-an-image-to-use-in-issue-comments-via-github-api
63-
64-
const s3url = `https://${s3.bucket}.s3.amazonaws.com/${relativePath}`;
65-
66-
const s3bucket = new S3();
67-
s3bucket.createBucket(() => {
68-
const params: S3.PutObjectRequest = {
69-
Key: relativePath,
70-
Body: attachmentBuffer,
71-
ContentType: mimeType === false ? null : mimeType,
72-
Bucket: s3.bucket,
73-
};
74-
75-
s3bucket.upload(params, function (err, data) {
76-
console.log(`\tUploaded ${basename} to ${s3url}`);
77-
if (err) {
78-
console.log('ERROR MSG: ', err);
79-
}
50+
if (s3 && s3.bucket) {
51+
const basename = path.basename(url);
52+
const extension = path.extname(url);
53+
const mimeType = mime.lookup(basename);
54+
const attachmentBuffer = await gitlabHelper.getAttachment(url);
55+
if (!attachmentBuffer) {
56+
continue;
57+
}
58+
59+
// // Generate file name for S3 bucket from URL
60+
const hash = crypto.createHash('sha256');
61+
hash.update(url);
62+
const newFileName = hash.digest('hex') + extension
63+
const relativePath = githubRepoId ? `${githubRepoId}/${newFileName}` : newFileName;
64+
// Doesn't seem like it is easy to upload an issue to github, so upload to S3
65+
//https://stackoverflow.com/questions/41581151/how-to-upload-an-image-to-use-in-issue-comments-via-github-api
66+
67+
const s3url = `https://${s3.bucket}.s3.amazonaws.com/${relativePath}`;
68+
69+
const s3bucket = new S3();
70+
s3bucket.createBucket(() => {
71+
const params: S3.PutObjectRequest = {
72+
Key: relativePath,
73+
Body: attachmentBuffer,
74+
ContentType: mimeType === false ? null : mimeType,
75+
Bucket: s3.bucket,
76+
};
77+
78+
s3bucket.upload(params, function (err, data) {
79+
console.log(`\tUploading ${basename} to ${s3url}... `);
80+
if (err) {
81+
console.log('ERROR: ', err);
82+
} else {
83+
console.log(`\t... success`);
84+
}
85+
});
8086
});
81-
});
82-
83-
// Add the new URL to the map
84-
offsetToAttachment[match.index] = `![${name}](${s3url})`;
85-
87+
88+
// Add the new URL to the map
89+
offsetToAttachment[match.index] = `![${name}](${s3url})`;
90+
} else {
91+
// Not using S3: default to old URL, adding absolute path
92+
const host = gitlabHelper.host.endsWith('/')
93+
? gitlabHelper.host : gitlabHelper.host + '/';
94+
const attachmentUrl = host + gitlabHelper.projectPath + url;
95+
offsetToAttachment[match.index] = `![${name}](${attachmentUrl})`;
96+
}
8697
}
8798

8899
return body.replace(regexp, ({},{},{},offset,{}) => offsetToAttachment[offset]);

0 commit comments

Comments
 (0)