From de80db1b9697a00b071ab4dd7ea9f4b1db32f4a3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 7 Sep 2019 20:42:01 -0700 Subject: [PATCH] Allow Image uploads through docker pictshare. - Fixes #210 --- ansible/templates/nginx.conf | 10 ++++++++++ docker/dev/docker-compose.yml | 7 +++++++ docker/prod/docker-compose.yml | 9 ++++++++- ui/src/components/comment-form.tsx | 29 +++++++++++++++++++++++++++-- ui/src/components/post-form.tsx | 28 +++++++++++++++++++++++++++- ui/src/components/post-listing.tsx | 7 ++++++- ui/src/utils.ts | 5 +++++ 7 files changed, 90 insertions(+), 5 deletions(-) diff --git a/ansible/templates/nginx.conf b/ansible/templates/nginx.conf index e9fef1816..ac4c3526e 100644 --- a/ansible/templates/nginx.conf +++ b/ansible/templates/nginx.conf @@ -46,6 +46,9 @@ server { add_header X-Frame-Options "DENY"; add_header X-XSS-Protection "1; mode=block"; + # Upload limit for pictshare + client_max_body_size 50M; + location / { rewrite (\/(user|u\/|inbox|post|community|c\/|create_post|create_community|login|search|setup|sponsors|communities|modlog|home)+) /static/index.html break; proxy_pass http://0.0.0.0:8536; @@ -57,5 +60,12 @@ server { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; + } + + location /pictshare/ { + proxy_pass http://0.0.0.0:8537/; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml index b7ab8c2bb..3f802ab28 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -22,5 +22,12 @@ services: - HOSTNAME=${DOMAIN} depends_on: - lemmy_db + lemmy_pictshare: + image: hascheksolutions/pictshare:latest + ports: + - "8537:80" + volumes: + - lemmy_pictshare:/usr/share/nginx/html/data volumes: lemmy_db: + lemmy_pictshare: diff --git a/docker/prod/docker-compose.yml b/docker/prod/docker-compose.yml index a4d9c7b89..74cf1c25c 100644 --- a/docker/prod/docker-compose.yml +++ b/docker/prod/docker-compose.yml @@ -20,5 +20,12 @@ services: - HOSTNAME=${DOMAIN} depends_on: - lemmy_db + lemmy_pictshare: + image: hascheksolutions/pictshare:latest + ports: + - "8537:80" + volumes: + - lemmy_pictshare:/usr/share/nginx/html/data volumes: - lemmy_db: + lemmy_db: + lemmy_pictshare: diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index e18a6e3ff..837cefb5b 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -136,7 +136,10 @@ export class CommentForm extends Component { } {this.props.node && } # - # +
+ + +
@@ -154,8 +157,8 @@ export class CommentForm extends Component { i.state.previewMode = false; i.state.commentForm.content = undefined; - i.setState(i.state); event.target.reset(); + i.setState(i.state); if (i.props.node) { i.props.onReplyCancel(); } @@ -177,6 +180,28 @@ export class CommentForm extends Component { handleReplyCancel(i: CommentForm) { i.props.onReplyCancel(); } + + handleImageUpload(i: CommentForm, event: any) { + event.preventDefault(); + let file = event.target.files[0]; + const imageUploadUrl = `/pictshare/api/upload.php`; + const formData = new FormData(); + formData.append('file', file); + fetch(imageUploadUrl, { + method: 'POST', + body: formData, + }) + .then(res => res.json()) + .then(res => { + let url = `${window.location.origin}/pictshare/${res.url}`; + let markdown = (res.filetype == 'mp4') ? `[vid](${url}/raw)` : `![](${url})`; + let content = i.state.commentForm.content; + content = (content) ? `${content} ${markdown}` : markdown; + i.state.commentForm.content = content; + i.setState(i.state); + }) + .catch((error) => alert(error)); + } userSearch(text: string, cb: any) { if (text) { diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 2e94e1772..1591dde09 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -109,7 +109,10 @@ export class PostForm extends Component { {this.state.suggestedTitle &&
#
} - # +
+ + +
{this.state.crossPosts.length > 0 && <>
#
@@ -266,6 +269,29 @@ export class PostForm extends Component { i.setState(i.state); } + handleImageUpload(i: PostForm, event: any) { + event.preventDefault(); + let file = event.target.files[0]; + const imageUploadUrl = `/pictshare/api/upload.php`; + const formData = new FormData(); + formData.append('file', file); + fetch(imageUploadUrl, { + method: 'POST', + body: formData, + }) + .then(res => res.json()) + .then(res => { + let url = `${window.location.origin}/pictshare/${res.url}`; + if (res.filetype == 'mp4') { + url += '/raw'; + } + + i.state.postForm.url = url; + i.setState(i.state); + }) + .catch((error) => alert(error)); + } + parseMessage(msg: any) { let op: UserOperation = msgOp(msg); if (msg.error) { diff --git a/ui/src/components/post-listing.tsx b/ui/src/components/post-listing.tsx index ccd3ce45f..5b83c74ee 100644 --- a/ui/src/components/post-listing.tsx +++ b/ui/src/components/post-listing.tsx @@ -4,7 +4,7 @@ import { WebSocketService, UserService } from '../services'; import { Post, CreatePostLikeForm, PostForm as PostFormI, SavePostForm, CommunityUser, UserView, BanType, BanFromCommunityForm, BanUserForm, AddModToCommunityForm, AddAdminForm, TransferSiteForm, TransferCommunityForm } from '../interfaces'; import { MomentTime } from './moment-time'; import { PostForm } from './post-form'; -import { mdToHtml, canMod, isMod, isImage, getUnixTime } from '../utils'; +import { mdToHtml, canMod, isMod, isImage, isVideo, getUnixTime } from '../utils'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -86,6 +86,11 @@ export class PostListing extends Component { {post.url && isImage(post.url) && } + {post.url && isVideo(post.url) && + + }
diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 2d5076a52..4deafd8b7 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -104,11 +104,16 @@ export function isMod(modIds: Array, creator_id: number): boolean { var imageRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`); +var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`); export function isImage(url: string) { return imageRegex.test(url); } +export function isVideo(url: string) { + return videoRegex.test(url); +} + export function validURL(str: string) { var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name