From e583e45d9a2221b3ed2a743cfa172abcd2a1d6a0 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 8 Jun 2020 19:52:32 +0200 Subject: [PATCH 01/23] Use pictrs instead of pictshare --- ansible/ansible.cfg | 1 + ansible/lemmy.yml | 10 +++--- ansible/lemmy_dev.yml | 7 ++-- ansible/templates/docker-compose.yml | 13 +++---- ansible/templates/nginx.conf | 8 ++--- docker/dev/docker-compose.yml | 52 ++++++++++++++++------------ docker/dev/nginx.conf | 40 +++++++++++++++++++++ ui/src/components/comment-form.tsx | 4 +-- ui/src/components/post-form.tsx | 6 ++-- ui/src/components/user.tsx | 4 +-- 10 files changed, 94 insertions(+), 51 deletions(-) create mode 100644 docker/dev/nginx.conf diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg index 960a7c40f..74b6ab2f1 100644 --- a/ansible/ansible.cfg +++ b/ansible/ansible.cfg @@ -1,5 +1,6 @@ [defaults] inventory=inventory +interpreter_python=/usr/bin/python3 [ssh_connection] pipelining = True diff --git a/ansible/lemmy.yml b/ansible/lemmy.yml index bc01623fc..dcafc5eac 100644 --- a/ansible/lemmy.yml +++ b/ansible/lemmy.yml @@ -24,10 +24,11 @@ creates: '/etc/letsencrypt/live/{{domain}}/privkey.pem' - name: create lemmy folder - file: path={{item.path}} state=directory + file: path={{item.path}} {{item.owner}} state=directory with_items: - - { path: '/lemmy/' } - - { path: '/lemmy/volumes/' } + - { path: '/lemmy/', owner: 'root' } + - { path: '/lemmy/volumes/', owner: 'root' } + - { path: '/lemmy/volumes/pictrs/', owner: '991' } - block: - name: add template files @@ -38,9 +39,6 @@ - { src: '../docker/iframely.config.local.js', dest: '/lemmy/iframely.config.local.js', mode: '0600' } vars: lemmy_docker_image: "dessalines/lemmy:{{ lookup('file', 'VERSION') }}" - lemmy_port: "8536" - pictshare_port: "8537" - iframely_port: "8538" - name: add config file (only during initial setup) template: src='templates/config.hjson' dest='/lemmy/lemmy.hjson' mode='0600' force='no' owner='1000' group='1000' diff --git a/ansible/lemmy_dev.yml b/ansible/lemmy_dev.yml index e9b8364f3..05eb1ffed 100644 --- a/ansible/lemmy_dev.yml +++ b/ansible/lemmy_dev.yml @@ -26,10 +26,11 @@ creates: '/etc/letsencrypt/live/{{domain}}/privkey.pem' - name: create lemmy folder - file: path={{item.path}} state=directory + file: path={{item.path}} owner={{item.owner}} state=directory with_items: - - { path: '/lemmy/' } - - { path: '/lemmy/volumes/' } + - { path: '/lemmy/', owner: 'root' } + - { path: '/lemmy/volumes/', owner: 'root' } + - { path: '/lemmy/volumes/pictrs/', owner: '991' } - block: - name: add template files diff --git a/ansible/templates/docker-compose.yml b/ansible/templates/docker-compose.yml index 9ec1bfbc2..0b33399bd 100644 --- a/ansible/templates/docker-compose.yml +++ b/ansible/templates/docker-compose.yml @@ -12,7 +12,7 @@ services: - ./lemmy.hjson:/config/config.hjson:ro depends_on: - postgres - - pictshare + - pictrs - iframely postgres: @@ -25,18 +25,19 @@ services: - ./volumes/postgres:/var/lib/postgresql/data restart: always - pictshare: - image: hascheksolutions/pictshare:latest + pictrs: + image: asonix/pictrs:amd64-v0.1.0-r9 + user: 991:991 ports: - - "127.0.0.1:8537:80" + - "127.0.0.1:8537:8080" volumes: - - ./volumes/pictshare:/usr/share/nginx/html/data + - ./volumes/pictrs:/mnt restart: always iframely: image: dogbin/iframely:latest ports: - - "127.0.0.1:8061:80" + - "127.0.0.1:8538:80" volumes: - ./iframely.config.local.js:/iframely/config.local.js:ro restart: always diff --git a/ansible/templates/nginx.conf b/ansible/templates/nginx.conf index a978c1899..b96bbce7f 100644 --- a/ansible/templates/nginx.conf +++ b/ansible/templates/nginx.conf @@ -70,19 +70,15 @@ server { proxy_cache_min_uses 5; } - location /pictshare/ { + location /pictrs/ { 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; - - if ($request_uri ~ \.(?:ico|gif|jpe?g|png|webp|bmp|mp4)$) { - add_header Cache-Control "public, max-age=31536000, immutable"; - } } location /iframely/ { - proxy_pass http://0.0.0.0:8061/; + proxy_pass http://0.0.0.0:8538/; 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 1702f66d3..6c0624c4b 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -1,6 +1,31 @@ version: '3.3' services: + nginx: + image: nginx:1.19-alpine + ports: + - "8536:8536" + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + depends_on: + - lemmy + - pictrs + - iframely + restart: "always" + + lemmy: + build: + context: ../../ + dockerfile: docker/dev/Dockerfile + restart: always + environment: + - RUST_LOG=debug + volumes: + - ../lemmy.hjson:/config/config.hjson + depends_on: + - postgres + - iframely + postgres: image: postgres:12-alpine environment: @@ -11,34 +36,15 @@ services: - ./volumes/postgres:/var/lib/postgresql/data restart: always - lemmy: - build: - context: ../../ - dockerfile: docker/dev/Dockerfile - ports: - - "127.0.0.1:8536:8536" - restart: always - environment: - - RUST_LOG=debug + pictrs: + image: asonix/pictrs:amd64-v0.1.0-r9 + user: 991:991 volumes: - - ../lemmy.hjson:/config/config.hjson - depends_on: - - postgres - - pictshare - - iframely - - pictshare: - image: hascheksolutions/pictshare:latest - ports: - - "127.0.0.1:8537:80" - volumes: - - ./volumes/pictshare:/usr/share/nginx/html/data + - ./volumes/pictrs:/mnt restart: always iframely: image: dogbin/iframely:latest - ports: - - "127.0.0.1:8061:80" volumes: - ../iframely.config.local.js:/iframely/config.local.js:ro restart: always diff --git a/docker/dev/nginx.conf b/docker/dev/nginx.conf new file mode 100644 index 000000000..3e4ff510e --- /dev/null +++ b/docker/dev/nginx.conf @@ -0,0 +1,40 @@ +events { + worker_connections 1024; +} + +http { + server { + listen 8536; + server_name 127.0.0.1; + #access_log off; + + # Upload limit for pictshare + client_max_body_size 50M; + + location / { + proxy_pass http://lemmy:8536; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + location /pictrs/ { + proxy_pass http://pictrs:8080/; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location /iframely/ { + proxy_pass http://iframely:80/; + 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/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index 5239eb2c7..7abab7524 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -304,7 +304,7 @@ export class CommentForm extends Component { file = event; } - const imageUploadUrl = `/pictshare/api/upload.php`; + const imageUploadUrl = `/pictrs/image`; const formData = new FormData(); formData.append('file', file); @@ -317,7 +317,7 @@ export class CommentForm extends Component { }) .then(res => res.json()) .then(res => { - let url = `${window.location.origin}/pictshare/${res.url}`; + let url = `${window.location.origin}/pictrs/${res.url}`; let imageMarkdown = res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`; let content = i.state.commentForm.content; diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 6f1e34e01..a9356d05d 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -518,9 +518,9 @@ export class PostForm extends Component { file = event; } - const imageUploadUrl = `/pictshare/api/upload.php`; + const imageUploadUrl = `/pictrs/image`; const formData = new FormData(); - formData.append('file', file); + formData.append('images', file); i.state.imageLoading = true; i.setState(i.state); @@ -531,7 +531,7 @@ export class PostForm extends Component { }) .then(res => res.json()) .then(res => { - let url = `${window.location.origin}/pictshare/${encodeURI(res.url)}`; + let url = `${window.location.origin}/pictrs/${encodeURI(res.url)}`; if (res.filetype == 'mp4') { url += '/raw'; } diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index eded9998c..c3b12fe02 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -988,7 +988,7 @@ export class User extends Component { handleImageUpload(i: User, event: any) { event.preventDefault(); let file = event.target.files[0]; - const imageUploadUrl = `/pictshare/api/upload.php`; + const imageUploadUrl = `/pictrs/image`; const formData = new FormData(); formData.append('file', file); @@ -1001,7 +1001,7 @@ export class User extends Component { }) .then(res => res.json()) .then(res => { - let url = `${window.location.origin}/pictshare/${res.url}`; + let url = `${window.location.origin}/pictrs/${res.url}`; if (res.filetype == 'mp4') { url += '/raw'; } From bd26e4e9c1b146163ee839de0a45bb9354efd1f2 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 9 Jun 2020 17:17:24 -0400 Subject: [PATCH 02/23] Fixing some front end pictshare to pictrs conversions. --- ansible/lemmy.yml | 3 +++ ansible/templates/docker-compose.yml | 2 +- ansible/templates/nginx.conf | 6 +++++- docker/dev/docker-compose.yml | 19 +++++++------------ ui/src/components/navbar.tsx | 6 +++--- ui/src/components/post-form.tsx | 20 +++++++++++++------- ui/src/components/post-listing.tsx | 10 +++++----- ui/src/components/private-message.tsx | 24 +++++++++++------------- ui/src/components/search.tsx | 2 +- ui/src/components/sidebar.tsx | 2 +- ui/src/components/user-listing.tsx | 4 ++-- ui/src/components/user.tsx | 21 +++++++++++++-------- ui/src/utils.ts | 25 ++++++++++++------------- 13 files changed, 77 insertions(+), 67 deletions(-) diff --git a/ansible/lemmy.yml b/ansible/lemmy.yml index dcafc5eac..0b49856ac 100644 --- a/ansible/lemmy.yml +++ b/ansible/lemmy.yml @@ -39,6 +39,9 @@ - { src: '../docker/iframely.config.local.js', dest: '/lemmy/iframely.config.local.js', mode: '0600' } vars: lemmy_docker_image: "dessalines/lemmy:{{ lookup('file', 'VERSION') }}" + lemmy_port: "8536" + pictshare_port: "8537" + iframely_port: "8538" - name: add config file (only during initial setup) template: src='templates/config.hjson' dest='/lemmy/lemmy.hjson' mode='0600' force='no' owner='1000' group='1000' diff --git a/ansible/templates/docker-compose.yml b/ansible/templates/docker-compose.yml index 0b33399bd..f4c94fd71 100644 --- a/ansible/templates/docker-compose.yml +++ b/ansible/templates/docker-compose.yml @@ -37,7 +37,7 @@ services: iframely: image: dogbin/iframely:latest ports: - - "127.0.0.1:8538:80" + - "127.0.0.1:8061:80" volumes: - ./iframely.config.local.js:/iframely/config.local.js:ro restart: always diff --git a/ansible/templates/nginx.conf b/ansible/templates/nginx.conf index b96bbce7f..68fa64fc5 100644 --- a/ansible/templates/nginx.conf +++ b/ansible/templates/nginx.conf @@ -75,10 +75,14 @@ server { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + if ($request_uri ~ \.(?:ico|gif|jpe?g|png|webp|bmp|mp4)$) { + add_header Cache-Control "public, max-age=31536000, immutable"; + } } location /iframely/ { - proxy_pass http://0.0.0.0:8538/; + proxy_pass http://0.0.0.0:8061/; 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 6c0624c4b..60b04beb8 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -1,22 +1,13 @@ version: '3.3' services: - nginx: - image: nginx:1.19-alpine - ports: - - "8536:8536" - volumes: - - ./nginx.conf:/etc/nginx/nginx.conf - depends_on: - - lemmy - - pictrs - - iframely - restart: "always" lemmy: build: context: ../../ dockerfile: docker/dev/Dockerfile + ports: + - "127.0.0.1:8536:8536" restart: always environment: - RUST_LOG=debug @@ -37,7 +28,9 @@ services: restart: always pictrs: - image: asonix/pictrs:amd64-v0.1.0-r9 + image: asonix/pictrs:v0.1.0-r13 + ports: + - "127.0.0.1:8537:8080" user: 991:991 volumes: - ./volumes/pictrs:/mnt @@ -45,6 +38,8 @@ services: iframely: image: dogbin/iframely:latest + ports: + - "127.0.0.1:8061:80" volumes: - ../iframely.config.local.js:/iframely/config.local.js:ro restart: always diff --git a/ui/src/components/navbar.tsx b/ui/src/components/navbar.tsx index e0d8aff50..4cb74391b 100644 --- a/ui/src/components/navbar.tsx +++ b/ui/src/components/navbar.tsx @@ -22,7 +22,7 @@ import { } from '../interfaces'; import { wsJsonToRes, - pictshareAvatarThumbnail, + pictrsAvatarThumbnail, showAvatars, fetchLimit, isCommentType, @@ -218,7 +218,7 @@ export class Navbar extends Component { {UserService.Instance.user.avatar && showAvatars() && ( { requestNotificationPermission() { if (UserService.Instance.user) { - document.addEventListener('DOMContentLoaded', function() { + document.addEventListener('DOMContentLoaded', function () { if (!Notification) { toast(i18n.t('notifications_error'), 'danger'); return; diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index a9356d05d..7811f9180 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -520,7 +520,7 @@ export class PostForm extends Component { const imageUploadUrl = `/pictrs/image`; const formData = new FormData(); - formData.append('images', file); + formData.append('images[]', file); i.state.imageLoading = true; i.setState(i.state); @@ -531,13 +531,19 @@ export class PostForm extends Component { }) .then(res => res.json()) .then(res => { - let url = `${window.location.origin}/pictrs/${encodeURI(res.url)}`; - if (res.filetype == 'mp4') { - url += '/raw'; + console.log('pictrs upload:'); + console.log(res); + if (res.msg == 'ok') { + let hash = res.files[0].file; + let url = `${window.location.origin}/pictrs/image/${hash}`; + i.state.postForm.url = url; + i.state.imageLoading = false; + i.setState(i.state); + } else { + i.state.imageLoading = false; + i.setState(i.state); + toast(JSON.stringify(res), 'danger'); } - i.state.postForm.url = url; - i.state.imageLoading = false; - i.setState(i.state); }) .catch(error => { i.state.imageLoading = false; diff --git a/ui/src/components/post-listing.tsx b/ui/src/components/post-listing.tsx index 36a1e2828..7d10acf72 100644 --- a/ui/src/components/post-listing.tsx +++ b/ui/src/components/post-listing.tsx @@ -28,7 +28,7 @@ import { isImage, isVideo, getUnixTime, - pictshareImage, + pictrsImage, setupTippy, previewLines, } from '../utils'; @@ -161,15 +161,15 @@ export class PostListing extends Component { getImage(thumbnail: boolean = false) { let post = this.props.post; if (isImage(post.url)) { - if (post.url.includes('pictshare')) { - return pictshareImage(post.url, thumbnail); + if (post.url.includes('pictrs')) { + return pictrsImage(post.url, thumbnail); } else if (post.thumbnail_url) { - return pictshareImage(post.thumbnail_url, thumbnail); + return pictrsImage(post.thumbnail_url, thumbnail); } else { return post.url; } } else if (post.thumbnail_url) { - return pictshareImage(post.thumbnail_url, thumbnail); + return pictrsImage(post.thumbnail_url, thumbnail); } } diff --git a/ui/src/components/private-message.tsx b/ui/src/components/private-message.tsx index 337b16501..71924f0cb 100644 --- a/ui/src/components/private-message.tsx +++ b/ui/src/components/private-message.tsx @@ -5,12 +5,7 @@ import { EditPrivateMessageForm, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { - mdToHtml, - pictshareAvatarThumbnail, - showAvatars, - toast, -} from '../utils'; +import { mdToHtml, pictrsAvatarThumbnail, showAvatars, toast } from '../utils'; import { MomentTime } from './moment-time'; import { PrivateMessageForm } from './private-message-form'; import { i18n } from '../i18next'; @@ -78,7 +73,7 @@ export class PrivateMessage extends Component< @@ -188,8 +184,9 @@ export class PrivateMessage extends Component< } > @@ -204,8 +201,9 @@ export class PrivateMessage extends Component< data-tippy-content={i18n.t('view_source')} > diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index b9662fae3..c32f4f2fb 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -22,7 +22,7 @@ import { fetchLimit, routeSearchTypeToEnum, routeSortTypeToEnum, - pictshareAvatarThumbnail, + pictrsAvatarThumbnail, showAvatars, toast, createCommentLikeRes, diff --git a/ui/src/components/sidebar.tsx b/ui/src/components/sidebar.tsx index fce315610..b280ef4fb 100644 --- a/ui/src/components/sidebar.tsx +++ b/ui/src/components/sidebar.tsx @@ -11,7 +11,7 @@ import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime, - pictshareAvatarThumbnail, + pictrsAvatarThumbnail, showAvatars, } from '../utils'; import { CommunityForm } from './community-form'; diff --git a/ui/src/components/user-listing.tsx b/ui/src/components/user-listing.tsx index 1f136e006..ea87fd3aa 100644 --- a/ui/src/components/user-listing.tsx +++ b/ui/src/components/user-listing.tsx @@ -1,7 +1,7 @@ import { Component } from 'inferno'; import { Link } from 'inferno-router'; import { UserView } from '../interfaces'; -import { pictshareAvatarThumbnail, showAvatars } from '../utils'; +import { pictrsAvatarThumbnail, showAvatars } from '../utils'; interface UserOther { name: string; @@ -25,7 +25,7 @@ export class UserListing extends Component { )} diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index c3b12fe02..833366a6e 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -990,7 +990,7 @@ export class User extends Component { let file = event.target.files[0]; const imageUploadUrl = `/pictrs/image`; const formData = new FormData(); - formData.append('file', file); + formData.append('images[]', file); i.state.avatarLoading = true; i.setState(i.state); @@ -1001,14 +1001,19 @@ export class User extends Component { }) .then(res => res.json()) .then(res => { - let url = `${window.location.origin}/pictrs/${res.url}`; - if (res.filetype == 'mp4') { - url += '/raw'; + console.log('pictrs upload:'); + console.log(res); + if (res.msg == 'ok') { + let hash = res.files[0].file; + let url = `${window.location.origin}/pictrs/image/${hash}`; + i.state.userSettingsForm.avatar = url; + i.state.avatarLoading = false; + i.setState(i.state); + } else { + i.state.avatarLoading = false; + i.setState(i.state); + toast(JSON.stringify(res), 'danger'); } - i.state.userSettingsForm.avatar = url; - console.log(url); - i.state.avatarLoading = false; - i.setState(i.state); }) .catch(error => { i.state.avatarLoading = false; diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 3bad50404..2820bc482 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -440,10 +440,12 @@ export function objectFlip(obj: any) { return ret; } -export function pictshareAvatarThumbnail(src: string): string { - // sample url: http://localhost:8535/pictshare/gs7xuu.jpg - let split = src.split('pictshare'); - let out = `${split[0]}pictshare/${canUseWebP() ? 'webp/' : ''}96${split[1]}`; +export function pictrsAvatarThumbnail(src: string): string { + // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg + let split = src.split('/pictrs/image'); + let out = `${split[0]}/pictrs/image/${ + canUseWebP() ? 'webp/' : '' + }thumbnail96${split[1]}`; return out; } @@ -455,21 +457,18 @@ export function showAvatars(): boolean { } // Converts to image thumbnail -export function pictshareImage( - hash: string, - thumbnail: boolean = false -): string { - let root = `/pictshare`; +export function pictrsImage(hash: string, thumbnail: boolean = false): string { + let root = `/pictrs/image`; // Necessary for other servers / domains - if (hash.includes('pictshare')) { - let split = hash.split('/pictshare/'); - root = `${split[0]}/pictshare`; + if (hash.includes('pictrs')) { + let split = hash.split('/pictrs/image/'); + root = `${split[0]}/pictrs/image`; hash = split[1]; } let out = `${root}/${canUseWebP() ? 'webp/' : ''}${ - thumbnail ? '192/' : '' + thumbnail ? 'thumbnail192/' : '' }${hash}`; return out; } From 2fbd44c59db9c94f7cd2055550066f429a19154b Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 10 Jun 2020 18:22:57 -0400 Subject: [PATCH 03/23] Adding pictrs thumbnail caching for urls and embeds. --- server/src/api/mod.rs | 2 +- server/src/api/post.rs | 4 ++-- server/src/lib.rs | 48 +++++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 3488a8c42..4f11b3278 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -18,7 +18,7 @@ use crate::db::user_mention_view::*; use crate::db::user_view::*; use crate::db::*; use crate::{ - extract_usernames, fetch_iframely_and_pictshare_data, generate_random_string, naive_from_unix, + extract_usernames, fetch_iframely_and_pictrs_data, generate_random_string, naive_from_unix, naive_now, remove_slurs, send_email, slur_check, slurs_vec_to_str, }; diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 84ef89f16..0e8a17e7c 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -118,7 +118,7 @@ impl Perform for Oper { // Fetch Iframely and Pictshare cached image let (iframely_title, iframely_description, iframely_html, pictshare_thumbnail) = - fetch_iframely_and_pictshare_data(data.url.to_owned()); + fetch_iframely_and_pictrs_data(data.url.to_owned()); let post_form = PostForm { name: data.name.to_owned(), @@ -452,7 +452,7 @@ impl Perform for Oper { // Fetch Iframely and Pictshare cached image let (iframely_title, iframely_description, iframely_html, pictshare_thumbnail) = - fetch_iframely_and_pictshare_data(data.url.to_owned()); + fetch_iframely_and_pictrs_data(data.url.to_owned()); let post_form = PostForm { name: data.name.to_owned(), diff --git a/server/src/lib.rs b/server/src/lib.rs index ca4bedea7..9b73caa8d 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -187,25 +187,35 @@ pub fn fetch_iframely(url: &str) -> Result { Ok(res) } -#[derive(Deserialize, Debug)] -pub struct PictshareResponse { - status: String, - url: String, +#[derive(Deserialize, Debug, Clone)] +pub struct PictrsResponse { + files: Vec, + msg: String, } -pub fn fetch_pictshare(image_url: &str) -> Result { +#[derive(Deserialize, Debug, Clone)] +pub struct PictrsFile { + file: String, + delete_token: String, +} + +pub fn fetch_pictrs(image_url: &str) -> Result { is_image_content_type(image_url)?; let fetch_url = format!( - "http://pictshare/api/geturl.php?url={}", - utf8_percent_encode(image_url, NON_ALPHANUMERIC) + "http://pictrs:8080/image/download?url={}", + utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed ); let text = isahc::get(&fetch_url)?.text()?; - let res: PictshareResponse = serde_json::from_str(&text)?; - Ok(res) + let res: PictrsResponse = serde_json::from_str(&text)?; + if res.msg == "ok" { + Ok(res) + } else { + Err(format_err!("{}", &res.msg)) + } } -fn fetch_iframely_and_pictshare_data( +fn fetch_iframely_and_pictrs_data( url: Option, ) -> ( Option, @@ -225,20 +235,20 @@ fn fetch_iframely_and_pictshare_data( } }; - // Fetch pictshare thumbnail - let pictshare_thumbnail = match iframely_thumbnail_url { - Some(iframely_thumbnail_url) => match fetch_pictshare(&iframely_thumbnail_url) { - Ok(res) => Some(res.url), + // Fetch pictrs thumbnail + let pictrs_thumbnail = match iframely_thumbnail_url { + Some(iframely_thumbnail_url) => match fetch_pictrs(&iframely_thumbnail_url) { + Ok(res) => Some(res.files[0].file.to_owned()), Err(e) => { - error!("pictshare err: {}", e); + error!("pictrs err: {}", e); None } }, // Try to generate a small thumbnail if iframely is not supported - None => match fetch_pictshare(&url) { - Ok(res) => Some(res.url), + None => match fetch_pictrs(&url) { + Ok(res) => Some(res.files[0].file.to_owned()), Err(e) => { - error!("pictshare err: {}", e); + error!("pictrs err: {}", e); None } }, @@ -248,7 +258,7 @@ fn fetch_iframely_and_pictshare_data( iframely_title, iframely_description, iframely_html, - pictshare_thumbnail, + pictrs_thumbnail, ) } None => (None, None, None, None), From 4cf1f080bf113d6e30383eb1fddd37ce16025558 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 10 Jun 2020 22:47:06 -0400 Subject: [PATCH 04/23] Adding delete picture via pict-rs delete tokens. Fixes #505 --- docker/dev/docker-compose.yml | 2 +- server/src/api/post.rs | 12 ++++----- ui/src/components/comment-form.tsx | 43 +++++++++++++++++++++--------- ui/src/components/post-form.tsx | 8 ++++++ ui/src/utils.ts | 23 ++++++++++++++++ ui/translations/en.json | 2 ++ 6 files changed, 70 insertions(+), 20 deletions(-) diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml index 60b04beb8..ab9ab7d82 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -28,7 +28,7 @@ services: restart: always pictrs: - image: asonix/pictrs:v0.1.0-r13 + image: asonix/pictrs:v0.1.3-r1 ports: - "127.0.0.1:8537:8080" user: 991:991 diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 0e8a17e7c..9eeb51580 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -116,8 +116,8 @@ impl Perform for Oper { return Err(APIError::err("site_ban").into()); } - // Fetch Iframely and Pictshare cached image - let (iframely_title, iframely_description, iframely_html, pictshare_thumbnail) = + // Fetch Iframely and pictrs cached image + let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = fetch_iframely_and_pictrs_data(data.url.to_owned()); let post_form = PostForm { @@ -135,7 +135,7 @@ impl Perform for Oper { embed_title: iframely_title, embed_description: iframely_description, embed_html: iframely_html, - thumbnail_url: pictshare_thumbnail, + thumbnail_url: pictrs_thumbnail, }; let inserted_post = match Post::create(&conn, &post_form) { @@ -450,8 +450,8 @@ impl Perform for Oper { return Err(APIError::err("site_ban").into()); } - // Fetch Iframely and Pictshare cached image - let (iframely_title, iframely_description, iframely_html, pictshare_thumbnail) = + // Fetch Iframely and Pictrs cached image + let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = fetch_iframely_and_pictrs_data(data.url.to_owned()); let post_form = PostForm { @@ -469,7 +469,7 @@ impl Perform for Oper { embed_title: iframely_title, embed_description: iframely_description, embed_html: iframely_html, - thumbnail_url: pictshare_thumbnail, + thumbnail_url: pictrs_thumbnail, }; let _updated_post = match Post::update(&conn, data.edit_id, &post_form) { diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index 7abab7524..79aa91bdd 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -18,6 +18,7 @@ import { setupTribute, wsJsonToRes, emojiPicker, + pictrsDeleteToast, } from '../utils'; import { WebSocketService, UserService } from '../services'; import autosize from 'autosize'; @@ -162,8 +163,9 @@ export class CommentForm extends Component { {this.state.commentForm.content && (