Add post file uploading and processing

- Add processing of videos via ffmpeg
- Add files for posts
master
trivernis 5 years ago
parent 2126ea1652
commit 526d861685

@ -70,7 +70,7 @@ export class FeedComponent implements OnInit {
*/ */
createPost(postElement, activityId: string) { createPost(postElement, activityId: string) {
if (postElement && activityId && this.checked) { if (postElement && activityId && this.checked) {
this.feedService.createPostActivity(postElement.value, activityId).subscribe(() => { this.feedService.createPostActivity(postElement.value, activityId, this.file).subscribe(() => {
postElement.value = ''; postElement.value = '';
this.textInputValue = ''; this.textInputValue = '';
this.checked = false; this.checked = false;
@ -82,7 +82,7 @@ export class FeedComponent implements OnInit {
this.errorMessage = error.error.errors[0].message; this.errorMessage = error.error.errors[0].message;
}); });
} else if (postElement) { } else if (postElement) {
this.feedService.createPost(postElement.value).subscribe(() => { this.feedService.createPost(postElement.value, this.file).subscribe(() => {
postElement.value = ''; postElement.value = '';
this.textInputValue = ''; this.textInputValue = '';
this.checked = false; this.checked = false;

@ -12,6 +12,7 @@ export class Post {
deletable: boolean; deletable: boolean;
author: Author; author: Author;
activity: Activity; activity: Activity;
mediaUrl: string;
constructor( constructor(
id: number, id: number,
@ -23,7 +24,8 @@ export class Post {
deletable: boolean, deletable: boolean,
date: string, date: string,
author: Author, author: Author,
activity: Activity activity: Activity,
mediaUrl?: string,
) { ) {
this.id = id; this.id = id;
this.content = content; this.content = content;
@ -35,5 +37,6 @@ export class Post {
this.date = date; this.date = date;
this.author = author; this.author = author;
this.activity = activity; this.activity = activity;
this.mediaUrl = mediaUrl;
} }
} }

@ -4,10 +4,11 @@ import {Post} from 'src/app/models/post';
import {Author} from 'src/app/models/author'; import {Author} from 'src/app/models/author';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import {Activity} from 'src/app/models/activity'; import {Activity} from 'src/app/models/activity';
import {BehaviorSubject} from 'rxjs'; import {BehaviorSubject, Observable} from 'rxjs';
import {tap} from 'rxjs/operators'; import {tap} from 'rxjs/operators';
import {BaseService} from '../base.service'; import {BaseService} from '../base.service';
import {formatDate} from '@angular/common'; import {formatDate} from '@angular/common';
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
const createPostGqlQuery = `mutation($content: String!) { const createPostGqlQuery = `mutation($content: String!) {
createPost(content: $content) { createPost(content: $content) {
@ -138,47 +139,68 @@ export class FeedService extends BaseService {
/** /**
* Creates a new post * Creates a new post
* @param pContent * @param pContent
* @param file
*/ */
public createPost(pContent: String) { public createPost(pContent: String, file?: File) {
const body = { const body = {
query: createPostGqlQuery, query: createPostGqlQuery,
variables: { variables: {
content: pContent content: pContent
} }
}; };
return this.createPostRequest(body); return this.createPostRequest(body, file);
} }
/** /**
* Creates a post with an activity * Creates a post with an activity
* @param pContent * @param pContent
* @param activityId * @param activityId
* @param file
*/ */
public createPostActivity(pContent: String, activityId: String) { public createPostActivity(pContent: String, activityId: String, file?: File) {
const body = { const body = {
query: createPostActivityGqlQuery, variables: { query: createPostActivityGqlQuery, variables: {
content: pContent, content: pContent,
id: activityId id: activityId
} }
}; };
return this.createPostRequest(body); return this.createPostRequest(body, file);
} }
/** /**
* Creates a new post with a given request. * Creates a new post with a given request.
* @param body * @param body
* @param file - a file that is being uploaded with the post
*/ */
private createPostRequest(body: { variables: any; query: string }) { private createPostRequest(body: { variables: any; query: string }, file?: File) {
return this.postGraphql(body, null, 0) return this.postGraphql(body, null, 0)
.pipe(tap(response => { .pipe(tap(response => {
if (this.activePostList === Sort.NEW) { if (this.activePostList === Sort.NEW) {
const updatedPosts = this.posts.getValue(); const updatedPosts = this.posts.getValue();
updatedPosts.unshift(this.constructPost(response)); const post = this.constructPost(response);
updatedPosts.unshift(post);
this.posts.next(updatedPosts); this.posts.next(updatedPosts);
if (file) {
this.uploadPostImage(post.id, file).subscribe((result) => {
post.mediaUrl = result.fileName;
});
}
} }
})); }));
} }
/**
* Uploads a file for a post
* @param postId
* @param file
*/
private uploadPostImage(postId: number, file: File): Observable<IFileUploadResult> {
const formData = new FormData();
formData.append('postMedia', file);
formData.append('postId', postId.toString());
return this.post<IFileUploadResult>(environment.greenvironmentUrl + '/upload', formData);
}
/** /**
* Upvotes a post * Upvotes a post
* @param postId * @param postId

Loading…
Cancel
Save