Skip to content

Commit 6c57383

Browse files
committed
Allow non-image attachments
1 parent 8591f52 commit 6c57383

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/utils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const generateUserProjectRegex = () => {
3434

3535
// Creates new attachments and replaces old links
3636
export const migrateAttachments = async (body: string, githubRepoId: number | undefined, s3: S3Settings, gitlabHelper: GitlabHelper) => {
37-
const regexp = /!\[([^\]]+)\]\((\/uploads[^)]+)\)/g;
37+
const regexp = /(!?)\[([^\]]+)\]\((\/uploads[^)]+)\)/g;
3838

3939
// Maps link offset to a new name in S3
4040
const offsetToAttachment: {
@@ -45,11 +45,12 @@ export const migrateAttachments = async (body: string, githubRepoId: number | un
4545
const matches = body.matchAll(regexp);
4646

4747
for (const match of matches) {
48-
const name = match[1];
49-
const url = match[2];
48+
const prefix = match[1] || '';
49+
const name = match[2];
50+
const url = match[3];
51+
5052
if (s3 && s3.bucket) {
5153
const basename = path.basename(url);
52-
const extension = path.extname(url);
5354
const mimeType = mime.lookup(basename);
5455
const attachmentBuffer = await gitlabHelper.getAttachment(url);
5556
if (!attachmentBuffer) {
@@ -59,7 +60,7 @@ export const migrateAttachments = async (body: string, githubRepoId: number | un
5960
// // Generate file name for S3 bucket from URL
6061
const hash = crypto.createHash('sha256');
6162
hash.update(url);
62-
const newFileName = hash.digest('hex') + extension
63+
const newFileName = hash.digest('hex') + '/' + basename
6364
const relativePath = githubRepoId ? `${githubRepoId}/${newFileName}` : newFileName;
6465
// Doesn't seem like it is easy to upload an issue to github, so upload to S3
6566
//https://stackoverflow.com/questions/41581151/how-to-upload-an-image-to-use-in-issue-comments-via-github-api
@@ -86,13 +87,13 @@ export const migrateAttachments = async (body: string, githubRepoId: number | un
8687
});
8788

8889
// Add the new URL to the map
89-
offsetToAttachment[match.index] = `![${name}](${s3url})`;
90+
offsetToAttachment[match.index] = `${prefix}[${name}](${s3url})`;
9091
} else {
9192
// Not using S3: default to old URL, adding absolute path
9293
const host = gitlabHelper.host.endsWith('/')
9394
? gitlabHelper.host : gitlabHelper.host + '/';
9495
const attachmentUrl = host + gitlabHelper.projectPath + url;
95-
offsetToAttachment[match.index] = `![${name}](${attachmentUrl})`;
96+
offsetToAttachment[match.index] = `${prefix}[${name}](${attachmentUrl})`;
9697
}
9798
}
9899

0 commit comments

Comments
 (0)