Markdown OKF-Schema Studio

Behaviour SwRS-MDOKFSTUDIO-PALETTE-196

The host saves a pasted image next to the document under a name it chooses

When previewPasteImage is accepted, the extension SHALL check the request and the image bytes, save the image in the folder of the document as image.png, image.jpg, image.gif or image.webp, or with the suffix -1, -2 and so on up to -1000 when the name is taken, comparing names without case, never showing the name before all its bytes are written, remove first the hidden partial files of its own naming that are older than 10 minutes, and answer the file name, or write nothing and answer a refusal with its reason when a check or the write fails.

Step 2 of path 1 in StRS-MDOKFSTUDIO-PALETTE-018: from “The host receives the image” to “The image is saved next to the document”.

Scenarios 7

The situations the tests check; open one to read it.

nominalThe host chooses the name

The name never comes from the page. The handler answers ok true and the file name (previewPasteImageResult). The envelope, the render gate and the answer correlation are those of SwRS-MDOKFSTUDIO-TRUST-052, SwRS-MDOKFSTUDIO-TRUST-065, SwRS-MDOKFSTUDIO-TRUST-005 and SwRS-MDOKFSTUDIO-TRUST-090.

GIVEN the folder of the documentWHEN the bytes areTHEN the file and the answer are
emptya pngimage.png
emptya jpegimage.jpg
emptya gifimage.gif
emptya webpimage.webp
holds image.pnga pngimage-1.png
holds image.png and image-1.pnga pngimage-2.png
holds a name that differs from image.png only by casea pngimage-1.png (the name counts as taken)
nominalThe name appears complete
Givena reader that looks at the folder during the write
a file created under the chosen name during the write
Whenthe image is being saved
the rename runs
Thenthe bytes go to a hidden partial file of the same folder (named dot, the file name, a random suffix and .partial) that is renamed to the chosen name without overwriting, so the chosen name is never seen empty or partial
that file is kept and not overwritten, and the image is saved under the next free name (image-1.png when image.png was taken), chosen again from a fresh listing of the folder, up to 5 attempts in all; the answer is then ok true with that name, or ok false with the error message when the rename keeps failing for another reason than a taken name
nominalA leftover of an interrupted save is removed
Givena folder holding a hidden file named dot, image (with an optional -number), a png, jpg, gif or webp extension, a random suffix of 1 to 8 lowercase letters or digits and .partial, last modified more than 10 minutes ago (left by a save that ended before its rename)
Whenan image is pasted into that folder
Thenthat file is deleted before the name is chosen; a file that matches the form but is newer, any file of another form and every other file of the folder are kept, and a file that cannot be removed does not stop the paste
boundaryThe numbering stops after 1000 suffixes
FolderOutcome
image-1000.png free, all smaller names takenthe file is written under image-1000.png
image.png and image-1.png to image-1000.png taken (1001 taken names)ok false with the reason Too many pasted images next to this document. and nothing is written
refusalA check fails

Each refusal answers ok false with the reason below and writes nothing; the folder is unchanged. The checks run in this order.

CaseReason
document with no file path, or whose id differs from the previewed documentSave the document before pasting an image.
saved document whose file is not a .md file (any case of the extension)Pasting an image needs a Markdown file.
document folder outside every workspace folder, both paths compared with symbolic links resolved; for a document that lies in no workspace folder at all, its own folder is the root, so the image is saved thereThe image must be saved inside a workspace folder.
announced type outside png, jpeg, gif and webp, or data that is not plain base64That clipboard image type is not supported.
more than 10 MiB (10 times 1024 times 1024 bytes) of bytesThe image is larger than 10 MB.
first bytes that are not those of the announced type (text announced as image/png)The clipboard data is not a png, jpeg, gif or webp image.
no free name up to the suffix 1000Too many pasted images next to this document.

A saved .md document in a folder inside the workspace, reached through a symbolic link or not, is accepted and the image is saved. This handler does not check the render revision.

refusalThe write fails
Givena write that fails
a write that succeeds
Whenthe save ends
the save ends
Thenno file, empty or partial, is left under the chosen name or the partial name, the partial file is deleted in every failing case, and the answer is ok false with the message of the error as the reason
only the chosen name remains
boundaryThe message shape
previewPasteImage fieldAcceptedOtherwise
mimeimage/png, image/jpeg, image/gif or image/webp (SVG is not accepted because it can carry script)the protocol rejects the message and the handler does not run
dataBase64a non-empty string of at most 14,000,000 characters in the base64 alphabet with optional padding and no whitespacethe protocol rejects the message and the handler does not run
edit idas for every host request (SwRS-MDOKFSTUDIO-TRUST-052)the protocol rejects the message

Implemented in 2

Tested by 6