Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File System Access API #1594

Closed
wants to merge 69 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
6b8cbf6
Create RockytheProtogen/FSApi.js
RockyTheProtogen Jul 7, 2024
a510e03
Made permission prompt to open file. Added close.
RockyTheProtogen Jul 8, 2024
2731111
I put the permission prompt in the wrong function, so I made a new one.
RockyTheProtogen Jul 8, 2024
9261736
Changed stuff for the last time today
RockyTheProtogen Jul 8, 2024
c545108
Code cleanup, just to keep it a little neater.
RockyTheProtogen Jul 8, 2024
dc9cacd
Revamped Stream read to implement delay and chunks
RockyTheProtogen Jul 8, 2024
4874840
Images, and fix FSApi breaking on write.
RockyTheProtogen Jul 8, 2024
2b09a46
Additional parameters for file open and labelling.
RockyTheProtogen Jul 8, 2024
1e7778f
Rename file to be more precise.
RockyTheProtogen Jul 8, 2024
541e129
Better contributions
RockyTheProtogen Jul 8, 2024
a5f946d
You can now open directories and view their contents.
RockyTheProtogen Jul 8, 2024
1952a47
Additional Changes
RockyTheProtogen Jul 9, 2024
b812909
Blank file.type is now checked and replaced with "unknown"
RockyTheProtogen Jul 9, 2024
b66ecc5
Local Validate, Lint, and Format
RockyTheProtogen Jul 9, 2024
fc53fb1
Added file slots
RockyTheProtogen Jul 9, 2024
0a54b08
Minimize null, and format
RockyTheProtogen Jul 9, 2024
1452616
Added /Folder/file.ext read
RockyTheProtogen Jul 9, 2024
035b648
Added Create/Delete File/Folder with reindexing option
RockyTheProtogen Jul 9, 2024
b717e3b
Fixed new block
RockyTheProtogen Jul 9, 2024
eb9f6a3
Note to self: Replace all breaks things
RockyTheProtogen Jul 9, 2024
f4c6b9f
Finishing up. Should be ready!
RockyTheProtogen Jul 10, 2024
cf54c4f
Remove Dev branch warning
RockyTheProtogen Jul 10, 2024
9e1069d
Code Cleanup 1
RockyTheProtogen Jul 10, 2024
2a0b019
Removed back to back if statements in place of mappings
RockyTheProtogen Jul 10, 2024
5a88e17
Validate, Lint, Format
RockyTheProtogen Jul 10, 2024
173a081
Removed lines
RockyTheProtogen Jul 10, 2024
d963309
Create NEW FSAAPI extension.
RockyTheProtogen Jul 10, 2024
61d6619
Added some quick CONST stuff
RockyTheProtogen Jul 10, 2024
bd41744
Preparing File/Folder data variables for later use
RockyTheProtogen Jul 10, 2024
8c0dbe3
Additional changes
RockyTheProtogen Jul 10, 2024
0d4c11c
Figuring out how to add Toaster-UI
RockyTheProtogen Jul 11, 2024
7becb32
Toaster spotted!?!?!
RockyTheProtogen Jul 11, 2024
0a5f1d2
Could not implement Toaster-UI.
RockyTheProtogen Jul 11, 2024
4714ad4
Note: Being tired does not help at all.
RockyTheProtogen Jul 11, 2024
72737ef
Local Format
RockyTheProtogen Jul 11, 2024
b0d3908
Push minor changes
RockyTheProtogen Jul 11, 2024
0503a20
"Good one-liners that will get you dismissed on the spot!"
RockyTheProtogen Jul 11, 2024
446bcfe
Stream read code dragged over.
RockyTheProtogen Jul 11, 2024
491a498
added 750 MB file hard limit
RockyTheProtogen Jul 11, 2024
0f20afe
I watched someone google "google", oh the pain.
RockyTheProtogen Jul 11, 2024
720bd01
"You'll live forever tonight" - ミラクルミュージカル
RockyTheProtogen Jul 11, 2024
abbafaa
Begin implementation of writing system
RockyTheProtogen Jul 11, 2024
c65eced
Added memory WRITE and PUSH
RockyTheProtogen Jul 11, 2024
bd36ff6
I configured stuff in vscode. Mostly ESLint and Prettier.
RockyTheProtogen Jul 12, 2024
f6f0fb8
added ::-da\\clear for clearing file
RockyTheProtogen Jul 12, 2024
5cf6340
Sneaky gitwatch trying to get through. Not on my watch.
RockyTheProtogen Jul 12, 2024
662dc79
Ah yes, an accidental nap
RockyTheProtogen Jul 12, 2024
b159c76
Read and write works as intended. (I think)
RockyTheProtogen Jul 12, 2024
ee3b5ca
Merge branch 'TurboWarp:master' into master
RockyTheProtogen Jul 13, 2024
2bc0413
Update for possible compliance in recent comments.
RockyTheProtogen Jul 13, 2024
ad73384
Prettier+ just made this thing slightly less readable to me
RockyTheProtogen Jul 13, 2024
4940289
Finish FILES
RockyTheProtogen Jul 13, 2024
f7b4b45
Begin work on folders
RockyTheProtogen Jul 13, 2024
cb51123
Idk
RockyTheProtogen Jul 13, 2024
ba53c79
Hmmm... yes... a commit
RockyTheProtogen Jul 14, 2024
908ec69
Run format and catch error in iGFC(iDH)
RockyTheProtogen Jul 15, 2024
1dac235
Read file stuff
RockyTheProtogen Jul 15, 2024
00f6261
Attempt 1 at squashing dumb bug
RockyTheProtogen Jul 15, 2024
5a3fef2
Fixed the bug
RockyTheProtogen Jul 15, 2024
51f1d92
Yippee
RockyTheProtogen Jul 16, 2024
8b968d6
Reuse existing code to create and delete files in folders
RockyTheProtogen Jul 18, 2024
9a8d6ce
more stuff, idk
RockyTheProtogen Jul 18, 2024
c558160
Whats grass?
RockyTheProtogen Jul 21, 2024
0355b0d
Merge branch 'TurboWarp:master' into master
RockyTheProtogen Aug 29, 2024
e3681de
Merge branch 'TurboWarp:master' into master
RockyTheProtogen Sep 15, 2024
b70b723
Start rewrite 3! Can't wait for 4 (jk last rewrite)
acemmerson Sep 15, 2024
aaf3b78
Yippee
acemmerson Sep 15, 2024
b61bd4a
Rewrite 3 Update 1
acemmerson Sep 16, 2024
1d86645
I forgot what I changed, but I added stuff
RockyTheProtogen Dec 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Local Format
RockyTheProtogen committed Jul 11, 2024
commit 72737ef1077a2d8b885e69f5675f9917a593f6a6
Original file line number Diff line number Diff line change
@@ -4,19 +4,13 @@
// By: Rocky the Protogen
// License: GNU-GPL3



/*
WARNING!
This version of the extension contains serious bugs that could hinder your project!
DO NOT use this version. It *will* break, and *will* fail.
This file is here for historical purposes.
*/





/**
* Major thanks to kindpump! <https://github.com/kindpump>
* Credits:
402 changes: 236 additions & 166 deletions extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js
Original file line number Diff line number Diff line change
@@ -4,182 +4,252 @@
// By: Rocky the Protogen
// License: GNU-GPL3


(function(Scratch) { //for some reason this was async. Changed it back.
'use strict';

//Define global varaibles

/* File Info */
let fileHandle1 = '', fileHandle2 = '', fileHandle3 = '', fileHandle4 = '', fileHandle5 = '';
let fimetadata1 = '', fimetadata2 = '', fimetadata3 = '', fimetadata4 = '', fimetadata5 = '';

/* Folder Info */
let folderHandle1 = '', folderHandle2 = '', folderHandle3 = '', folderHandle4 = '', folderHandle5 = '';
let fometadata1 = '', fometadata2 = '', fometadata3 = '', fometadata4 = '', fometadata5 = '';

//Define global const
const app = {hasFSAccess: "chooseFileSystemEntries" in window || "showOpenFilePicker" in window};
const bt = {reporter: Scratch.BlockType.REPORTER, boolean: Scratch.BlockType.BOOLEAN, command: Scratch.BlockType.COMMAND, label: Scratch.BlockType.LABEL, button: Scratch.BlockType.BUTTON}
const conv = new Scratch.Cast()
const cs = console
const idb = window.indexedDB


const argt = Scratch.ArgumentType.STRING
const fislotmenu = ['1','2','3','4','5']
const fislot = {1: {file: fileHandle1, metadata: fimetadata1},2: {file: fileHandle2, metadata: fimetadata2},3: {file: fileHandle3, metadata: fimetadata3},4: {file: fileHandle4, metadata: fimetadata4},5: {file: fileHandle5, metadata: fimetadata5}}
const foslot = {1: {file: folderHandle1,metadata: fometadata1},2: {file: folderHandle2,metadata: fometadata2},3: {file: folderHandle3,metadata: fometadata3},4: {file: folderHandle4,metadata: fometadata4},5: {file: folderHandle5,metadata: fometadata5}}

//Checks
if (!Scratch.extensions.unsandboxed) throw new Error('File System Access API must run unsandboxed');
if (!app.hasFSAccess) alert('Browser is incompatible: API not found.\nBlocks will not function.');

//Check extensions
let LoadedExtensions = JSON.stringify(Array.from(Scratch.vm.extensionManager._loadedExtensions.keys()));

//WOAH
// 2 Classes? Yes!
// Why? Separation.
// Or, more specifically, to help the user recognise functions more easily.
// Doesn't say I can't... so I will.
class files {
getInfo() {
return {
id: 'filesystemaccessapiv2files',
name: 'FSA Files',
blocks: [
{
opcode: 'openFile',
blockType: bt.command,
text: 'In FILE slot [num] open a file',
arguments: {
num: {
type: argt,
menu: 'num'
}
}
(function (Scratch) {
//for some reason this was async. Changed it back.
"use strict";

//Define global varaibles

/* File Info */
let fileHandle1 = "",
fileHandle2 = "",
fileHandle3 = "",
fileHandle4 = "",
fileHandle5 = "";
let fimetadata1 = "",
fimetadata2 = "",
fimetadata3 = "",
fimetadata4 = "",
fimetadata5 = "";

/* Folder Info */
let folderHandle1 = "",
folderHandle2 = "",
folderHandle3 = "",
folderHandle4 = "",
folderHandle5 = "";
let fometadata1 = "",
fometadata2 = "",
fometadata3 = "",
fometadata4 = "",
fometadata5 = "";

//Define global const
const app = {
hasFSAccess:
"chooseFileSystemEntries" in window || "showOpenFilePicker" in window,
};
const bt = {
reporter: Scratch.BlockType.REPORTER,
boolean: Scratch.BlockType.BOOLEAN,
command: Scratch.BlockType.COMMAND,
label: Scratch.BlockType.LABEL,
button: Scratch.BlockType.BUTTON,
};
const conv = new Scratch.Cast();

Check failure on line 49 in extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js

GitHub Actions / lint

'conv' is assigned a value but never used. Allowed unused vars must match /^_/u
const cs = console;
const idb = window.indexedDB;

Check failure on line 51 in extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js

GitHub Actions / lint

'idb' is assigned a value but never used. Allowed unused vars must match /^_/u

const argt = Scratch.ArgumentType.STRING;
const fislotmenu = ["1", "2", "3", "4", "5"];
const fislot = {
1: { file: fileHandle1, metadata: fimetadata1 },
2: { file: fileHandle2, metadata: fimetadata2 },
3: { file: fileHandle3, metadata: fimetadata3 },
4: { file: fileHandle4, metadata: fimetadata4 },
5: { file: fileHandle5, metadata: fimetadata5 },
};
const foslot = {

Check failure on line 62 in extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js

GitHub Actions / lint

'foslot' is assigned a value but never used. Allowed unused vars must match /^_/u
1: { file: folderHandle1, metadata: fometadata1 },
2: { file: folderHandle2, metadata: fometadata2 },
3: { file: folderHandle3, metadata: fometadata3 },
4: { file: folderHandle4, metadata: fometadata4 },
5: { file: folderHandle5, metadata: fometadata5 },
};

//Checks
if (!Scratch.extensions.unsandboxed)
throw new Error("File System Access API must run unsandboxed");
if (!app.hasFSAccess)
alert("Browser is incompatible: API not found.\nBlocks will not function.");

//Check extensions
let LoadedExtensions = JSON.stringify(
Array.from(Scratch.vm.extensionManager._loadedExtensions.keys())
);

//WOAH
// 2 Classes? Yes!
// Why? Separation.
// Or, more specifically, to help the user recognise functions more easily.
// Doesn't say I can't... so I will.
class files {
getInfo() {
return {
id: "filesystemaccessapiv2files",
name: "FSA Files",
blocks: [
{
opcode: "openFile",
blockType: bt.command,
text: "In FILE slot [num] open a file",
arguments: {
num: {
type: argt,
menu: "num",
},
},
{
opcode: 'cancel',
blockType: bt.command,
text: 'Empty FILE slot [num]',
arguments: {
num: {
type: argt,
menu: 'num'
}
}
},
{
opcode: "cancel",
blockType: bt.command,
text: "Empty FILE slot [num]",
arguments: {
num: {
type: argt,
menu: "num",
},
},
'---',
{
opcode: 'metadata',
blockType: bt.reporter,
text: 'FILE slot [num]\'s metadata',
arguments: {
num: {
type: argt,
menu: 'num'
}
}
},
"---",
{
opcode: "metadata",
blockType: bt.reporter,
text: "FILE slot [num]'s metadata",
arguments: {
num: {
type: argt,
menu: "num",
},
},
{
opcode: 'getData',
blockType: bt.reporter,
text: 'Read FILE slot [num] with method [TYPE]',
arguments: {
num: {
type: argt,
menu: 'num'
},
TYPE: {
type: argt,
menu: 'methods'
}
}
},
{
opcode: "getData",
blockType: bt.reporter,
text: "Read FILE slot [num] with method [TYPE]",
arguments: {
num: {
type: argt,
menu: "num",
},
TYPE: {
type: argt,
menu: "methods",
},
},
},
],
menus: {
num: {
acceptReporters: true,
items: fislotmenu
},
methods: {
acceptReporters: true,
items: ['stream','arrayBuffer','text','slice']
}
}
};
}
//Functions
async openFile(args) { //Shows the file picker and gets file information.
const slot = fislot[args.num];
try {
if ((slot.file == '') && (slot.metadata == '')) {
[slot.file] = await window.showOpenFilePicker()
cs.log(slot.file)
slot.metadata = await slot.file.getFile()
cs.log(slot.metadata)
if (slot.metadata.size >= 25000000) if (!confirm('This file exceeds 25 MB and could cause problems.\nIf you wish to continue, press OK.\nOtherwise, press cancel.')) await this.cancel(slot).then(cs.error('User has quit import.'));
}
} catch (err) {
cs.error('\nFailed to open.\n\nDetails:\n' + err)
return 'Failed to open.\nDetails:\n' + err;
}
}

metadata(args) {
const slot = fislot[args.num];
return JSON.stringify({name: slot.metadata.name,lastModified: slot.metadata.lastModified,lastModifiedDate: slot.metadata.lastModifiedDate,webkitRelativePath: slot.metadata.webkitRelativePath,size: slot.metadata.size,type: slot.metadata.type});
menus: {
num: {
acceptReporters: true,
items: fislotmenu,
},
methods: {
acceptReporters: true,
items: ["stream", "arrayBuffer", "text", "slice"],
},
},
};
}
//Functions
async openFile(args) {
//Shows the file picker and gets file information.
const slot = fislot[args.num];
try {
if (slot.file == "" && slot.metadata == "") {
[slot.file] = await window.showOpenFilePicker();
cs.log(slot.file);
slot.metadata = await slot.file.getFile();
cs.log(slot.metadata);
if (slot.metadata.size >= 25000000)
if (
!confirm(
"This file exceeds 25 MB and could cause problems.\nIf you wish to continue, press OK.\nOtherwise, press cancel."
)
)
await this.cancel(slot).then(cs.error("User has quit import."));
}
} catch (err) {
cs.error("\nFailed to open.\n\nDetails:\n" + err);
return "Failed to open.\nDetails:\n" + err;
}
}

cancel(args,CurrentSlot) {
function recogslot(Block,Func) {if (!Block) {return Func} else {return fislot[Block]}}
const slot = recogslot(args.num,CurrentSlot)
slot.file = '';
slot.metadata = '';
}
metadata(args) {
const slot = fislot[args.num];
return JSON.stringify({
name: slot.metadata.name,
lastModified: slot.metadata.lastModified,
lastModifiedDate: slot.metadata.lastModifiedDate,
webkitRelativePath: slot.metadata.webkitRelativePath,
size: slot.metadata.size,
type: slot.metadata.type,
});
}

async getData(args) {
const slot = fislot[args.num];
cancel(args, CurrentSlot) {
function recogslot(Block, Func) {
if (!Block) {
return Func;
} else {
return fislot[Block];
}
}

}
const slot = recogslot(args.num, CurrentSlot);
slot.file = "";
slot.metadata = "";
}

class folders {
getInfo() {
return {
id: 'filesystemaccessapiv2folders',
name: 'FSA Folders',
blocks: [
{
opcode: 'openFolder',
blockType: bt.command,
text: 'In FOLDER slot [num] open a folder',
arguments: {
num: {
type: argt,
menu: 'num'
}
}
}
],
menus: {
num: {
acceptReporters: true,
items: fislotmenu
}
}
async getData(args) {

Check failure on line 203 in extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js

GitHub Actions / lint

Async method 'getData' has no 'await' expression
const slot = fislot[args.num];

Check failure on line 204 in extensions/RockytheProtogen/FileSystemAccessAPI/FSA-API-TW-Version2.js

GitHub Actions / lint

'slot' is assigned a value but never used. Allowed unused vars must match /^_/u
}
}

class folders {
getInfo() {
return {
id: "filesystemaccessapiv2folders",
name: "FSA Folders",
blocks: [
{
opcode: "openFolder",
blockType: bt.command,
text: "In FOLDER slot [num] open a folder",
arguments: {
num: {
type: argt,
menu: "num",
},
},
},
],
menus: {
num: {
acceptReporters: true,
items: fislotmenu,
},
},
};
//Functions
}
}

if (!LoadedExtensions.includes("skyhigh173JSON")) Scratch.vm.extensionManager.loadExtensionURL("https://extensions.turbowarp.org/Skyhigh173/json.js")
try {
Scratch.extensions.register(new files())
Scratch.extensions.register(new folders())
} catch (err) {
alert('Failed to load a module!\nPlease report this issue.\nDetails:\n' + err);
}
})(Scratch);
}

if (!LoadedExtensions.includes("skyhigh173JSON"))
Scratch.vm.extensionManager.loadExtensionURL(
"https://extensions.turbowarp.org/Skyhigh173/json.js"
);
try {
Scratch.extensions
.register(new files())
.then(
cs.log("File System Access API files module successfully registered.")
);
Scratch.extensions
.register(new folders())
.then(cs.log("File System Access API folders successfully registered."));
} catch (err) {
alert(
"Failed to load a module!\nPlease report this issue.\nDetails:\n" + err
);
}
})(Scratch);