forked from piceaTech/node-gitlab-2-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
93 lines (81 loc) · 2.85 KB
/
utils.ts
File metadata and controls
93 lines (81 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { S3Settings } from './settings';
import * as mime from 'mime-types';
import * as path from 'path';
import * as crypto from 'crypto';
import S3 from 'aws-sdk/clients/s3';
import { GitlabHelper } from './gitlabHelper';
export const sleep = (milliseconds: number) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
// Creates new attachments and replaces old links
export const migrateAttachments = async (
body: string,
githubRepoId: number | undefined,
s3: S3Settings | undefined,
gitlabHelper: GitlabHelper
) => {
const regexp = /(!?)\[([^\]]+)\]\((\/uploads[^)]+)\)/g;
// Maps link offset to a new name in S3
const offsetToAttachment: {
[key: number]: string;
} = {};
// Find all local links
const matches = body.matchAll(regexp);
for (const match of matches) {
const prefix = match[1] || '';
const name = match[2];
const url = match[3];
if (s3 && s3.bucket) {
const basename = path.basename(url);
const mimeType = mime.lookup(basename);
const attachmentBuffer = await gitlabHelper.getAttachment(url);
if (!attachmentBuffer) {
continue;
}
// // Generate file name for S3 bucket from URL
const hash = crypto.createHash('sha256');
hash.update(url);
const newFileName = hash.digest('hex') + '/' + basename;
const relativePath = githubRepoId
? `${githubRepoId}/${newFileName}`
: newFileName;
// Doesn't seem like it is easy to upload an issue to github, so upload to S3
//https://stackoverflow.com/questions/41581151/how-to-upload-an-image-to-use-in-issue-comments-via-github-api
const s3url = `https://${s3.bucket}.s3.amazonaws.com/${relativePath}`;
const s3bucket = new S3();
s3bucket.createBucket(() => {
const params: S3.PutObjectRequest = {
Key: relativePath,
Body: attachmentBuffer,
ContentType: mimeType === false ? undefined : mimeType,
Bucket: s3.bucket,
};
s3bucket.upload(params, function (err, data) {
console.log(`\tUploading ${basename} to ${s3url}... `);
if (err) {
console.log('ERROR: ', err);
} else {
console.log(`\t...Done uploading`);
}
});
});
// Add the new URL to the map
offsetToAttachment[
match.index as number
] = `${prefix}[${name}](${s3url})`;
} else {
// Not using S3: default to old URL, adding absolute path
const host = gitlabHelper.host.endsWith('/')
? gitlabHelper.host
: gitlabHelper.host + '/';
const attachmentUrl = host + gitlabHelper.projectPath + url;
offsetToAttachment[
match.index as number
] = `${prefix}[${name}](${attachmentUrl})`;
}
}
return body.replace(
regexp,
({}, {}, {}, {}, offset, {}) => offsetToAttachment[offset]
);
};