openvibers/openvibe.mediahub Commits
A picture alone is a comment on a video (#426)
Changes
@@@ -12,10 +12,11 @@ async function readCommentForm(req) {const request = new Request('http://localhost/comment', { method: 'POST', headers: { 'content-type': req.get('content-type') }, body: req.body });const form = await request.formData();const message = String(form.get('message') || '').trim();-- if (!message || message.length > 2000) throw new Error('A comment is 1 to 2,000 characters');++ const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);++ // A picture alone is a comment, as on any social app (the owner, 11-12): words are optional with a file attached.++ if ((!message && !files.length) || message.length > 2000) throw new Error('Write something or attach a picture (up to 2,000 characters)');const parent = String(form.get('parent_id') || '');if (parent && !ID.test(parent)) throw new Error('That reply target is not valid');-- const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);if (files.length > 4) throw new Error('Attach up to four files');for (const file of files) {if (file.size > MAX_FILE) throw new Error('A file can be up to 16 MB');nnew file mode 100644@@@ -0,0 +1,21 @@++'use strict';++// The video page's comment form: a picture alone is a comment (the owner, 11-12), words alone are, nothing is not.++const assert = require('assert');++const { readCommentForm } = require('../server/comments/form');++async function reqOf(fields) {++ const fd = new FormData();++ for (const [k, v] of fields) fd.append(k, v);++ const r = new Request('http://x/', { method: 'POST', body: fd });++ const body = Buffer.from(await r.arrayBuffer());++ return { body, get: (h) => (h.toLowerCase() === 'content-type' ? r.headers.get('content-type') : null) };++}++(async () => {++ const pic = new Blob([Buffer.from('png bytes')], { type: 'image/png' });++ const onlyPic = await readCommentForm(await reqOf([['message', ''], ['files', new File([pic], 'a.png', { type: 'image/png' })]]));++ assert.deepStrictEqual([onlyPic.message, onlyPic.files.length], ['', 1], 'a picture with no caption');++ const words = await readCommentForm(await reqOf([['message', ' nice ']]));++ assert.deepStrictEqual([words.message, words.files.length], ['nice', 0]);++ await assert.rejects(readCommentForm(await reqOf([['message', ' ']])), /Write something or attach a picture/);++ await assert.rejects(readCommentForm(await reqOf([['message', 'x'.repeat(2001)]])), /up to 2,000 characters/);++ console.log('comment form: a picture alone, words alone, never nothing');++})().catch((e) => { console.error(e); process.exit(1); });
@@ -12,10 +12,11 @@ async function readCommentForm(req) {
@@ -12,10 +12,11 @@ async function readCommentForm(req) {
const request = new Request('http://localhost/comment', { method: 'POST', headers: { 'content-type': req.get('content-type') }, body: req.body });
const request = new Request('http://localhost/comment', { method: 'POST', headers: { 'content-type': req.get('content-type') }, body: req.body });
const form = await request.formData();
const form = await request.formData();
const message = String(form.get('message') || '').trim();
const message = String(form.get('message') || '').trim();
- if (!message || message.length > 2000) throw new Error('A comment is 1 to 2,000 characters');
+ const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);
+ // A picture alone is a comment, as on any social app (the owner, 11-12): words are optional with a file attached.
+ if ((!message && !files.length) || message.length > 2000) throw new Error('Write something or attach a picture (up to 2,000 characters)');
const parent = String(form.get('parent_id') || '');
const parent = String(form.get('parent_id') || '');
if (parent && !ID.test(parent)) throw new Error('That reply target is not valid');
if (parent && !ID.test(parent)) throw new Error('That reply target is not valid');
- const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);
if (files.length > 4) throw new Error('Attach up to four files');
if (files.length > 4) throw new Error('Attach up to four files');
for (const file of files) {
for (const file of files) {
if (file.size > MAX_FILE) throw new Error('A file can be up to 16 MB');
if (file.size > MAX_FILE) throw new Error('A file can be up to 16 MB');
new file mode 100644
new file mode 100644
@@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
+'use strict';
+// The video page's comment form: a picture alone is a comment (the owner, 11-12), words alone are, nothing is not.
+const assert = require('assert');
+const { readCommentForm } = require('../server/comments/form');
+async function reqOf(fields) {
+ const fd = new FormData();
+ for (const [k, v] of fields) fd.append(k, v);
+ const r = new Request('http://x/', { method: 'POST', body: fd });
+ const body = Buffer.from(await r.arrayBuffer());
+ return { body, get: (h) => (h.toLowerCase() === 'content-type' ? r.headers.get('content-type') : null) };
+}
+(async () => {
+ const pic = new Blob([Buffer.from('png bytes')], { type: 'image/png' });
+ const onlyPic = await readCommentForm(await reqOf([['message', ''], ['files', new File([pic], 'a.png', { type: 'image/png' })]]));
+ assert.deepStrictEqual([onlyPic.message, onlyPic.files.length], ['', 1], 'a picture with no caption');
+ const words = await readCommentForm(await reqOf([['message', ' nice ']]));
+ assert.deepStrictEqual([words.message, words.files.length], ['nice', 0]);
+ await assert.rejects(readCommentForm(await reqOf([['message', ' ']])), /Write something or attach a picture/);
+ await assert.rejects(readCommentForm(await reqOf([['message', 'x'.repeat(2001)]])), /up to 2,000 characters/);
+ console.log('comment form: a picture alone, words alone, never nothing');
+})().catch((e) => { console.error(e); process.exit(1); });