Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions src/app/services/mastodon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,43 @@ export class MastodonService {
}

uploadMediaAttachment(account: AccountInfo, file: File, description: string): Promise<Attachment> {
let input = new FormData();
input.append('file', file);
if (description !== null && description !== undefined) {
input.append('description', description);
} else {
input.append('description', '');
}
const route = `https://${account.instance}${this.apiRoutes.uploadMediaAttachment}`;
const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` });
return this.httpClient.post<Attachment>(route, input, { headers: headers }).toPromise();
return new Promise((resolve, reject) => {
let input = new FormData();
input.append('file', file);
if (description !== null && description !== undefined) {
input.append('description', description);
} else {
input.append('description', '');
}
const route = `https://${account.instance}${this.apiRoutes.uploadMediaAttachment}`;
const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` });

this.httpClient.post<Attachment>(route, input, { headers: headers, observe: 'response' })
.subscribe(response => {
if (response.status === 202) {
let tryCount = 1;
const checkMediaStatus = () => {
this.httpClient.get<Attachment>(`https://${account.instance}${this.apiRoutes.updateMediaAttachment.replace('{0}', response.body.id)}`, { headers: headers })
.subscribe(mediaStatus => {
if (mediaStatus.url) {
resolve(mediaStatus);
} else {
const retryAfter = (Math.log2(tryCount) || 1) * 1000;
tryCount += 1;
setTimeout(checkMediaStatus, retryAfter);
}
}, error => {
reject(error);
});
};
checkMediaStatus();
} else {
resolve(response.body);
}
}, error => {
reject(error);
});
});
}

//TODO: add focus support
Expand Down Expand Up @@ -704,4 +731,4 @@ export class FollowingResult {
constructor(
public maxId: string,
public follows: Account[]) { }
}
}
2 changes: 1 addition & 1 deletion src/app/services/models/api.settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ApiRoutes {
followRemote = '/api/v1/follows';
getInstance = '/api/v1/instance';
getInstancev2 = '/api/v2/instance';
uploadMediaAttachment = '/api/v1/media';
uploadMediaAttachment = '/api/v2/media';
updateMediaAttachment = '/api/v1/media/{0}';
getMutes = '/api/v1/mutes';
getNotifications = '/api/v1/notifications';
Expand Down