Sources

For a JavaScript solution for either client-side or server-side, you can use Node.js; however,

  • client side requires a package called: "NWJS" available here: http://nwjs.io/
  • server side only requires "Node.js" available here: https://nodejs.org

You can find extensive documentation on either of these "JavaScript" solutions; however, there are other "JavaScript" solutions available, NodeJS is very popular.

If you work with another language on the server, like PHP, you can find more info about it here: http://php.net



Solution

The following describes a JavaScript solution with code for server-side that you can just copy & paste and modify to your needs.

This assumes you are running NodeJs on Linux and that the file/folder (path) is not recursive. The example below is not tested, feel free to test & fix as necessary.

For the client-side code interacting with the "server-side" example below, create an HTML form that uses: method="PUT" and the fields as required by the vars; -OR- use an AJAX method to accomplish the same.

Server-side: NodeJS

let http = require('http');
//File System package...
let fsys = require('fs');


let makePath = function(root, path, data)
{
    try
    {
        fsys.accessSync(root, fsys.W_OK);
    }
    catch(err)
    {
        return {code:403, text:'Forbidden'}
    }

    path = ((path[0] == '/') ? path.substr(1, path.length) : path);

    if (path.split('/').length > 2)
    { return {code:412, text:'Precondition Failed'}; }

    if (fsys.existsSync(path))
    { return {code:409, text:'Conflict'}; }

    if (path[path.length -1] == '/')
    { fsys.mkdirSync(root +'/'+ path.substr(0, path.length -2)); }
    else
    { fsys.writeFileSync((root +'/'+ path), (data || ' '), 'utf8'); }

    return {code:200, text:'OK'};
};


http.createServer
(
    function(request, response)
    {
        let vars = url.parse(request.url);

        if (path && (path.indexOf('/') > -1) && (request.method == 'PUT'))
        {
            var resp = makePath(__dirname, vars.path, vars.data);

            response.statusCode = resp.code;
            response.setHeader('Content-Type', 'text/plain');
            response.end(resp.text);
        }
    }
).listen(8124);

Usage

You can access this from your web browser, if your server runs on the same machine, in your web-browser's address bar type: http://127.0.0.1:8124 and hit enter/return; however, please see the proper NodeJS documentation for serving the necessary client-side HTML & JavaScript as mentioned.

Answer from user4244405 on Stack Overflow
🌐
Peergos
book.peergos.org › features › secret.html
Secret links
A secret link can be generated to point to any file or folder. Anyone with a (Javascript enabled) web browser can view such a link. Secret links can be read-only, writable, time limited, password-protected or n-use. Secret links can be revoked. A secret link doesn't expose the file to the network, ...
🌐
GitHub
github.com › peergos › peergos
GitHub - Peergos/Peergos: A p2p, secure file storage, social network and application protocol · GitHub
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
Top answer
1 of 1
3

Sources

For a JavaScript solution for either client-side or server-side, you can use Node.js; however,

  • client side requires a package called: "NWJS" available here: http://nwjs.io/
  • server side only requires "Node.js" available here: https://nodejs.org

You can find extensive documentation on either of these "JavaScript" solutions; however, there are other "JavaScript" solutions available, NodeJS is very popular.

If you work with another language on the server, like PHP, you can find more info about it here: http://php.net



Solution

The following describes a JavaScript solution with code for server-side that you can just copy & paste and modify to your needs.

This assumes you are running NodeJs on Linux and that the file/folder (path) is not recursive. The example below is not tested, feel free to test & fix as necessary.

For the client-side code interacting with the "server-side" example below, create an HTML form that uses: method="PUT" and the fields as required by the vars; -OR- use an AJAX method to accomplish the same.

Server-side: NodeJS

let http = require('http');
//File System package...
let fsys = require('fs');


let makePath = function(root, path, data)
{
    try
    {
        fsys.accessSync(root, fsys.W_OK);
    }
    catch(err)
    {
        return {code:403, text:'Forbidden'}
    }

    path = ((path[0] == '/') ? path.substr(1, path.length) : path);

    if (path.split('/').length > 2)
    { return {code:412, text:'Precondition Failed'}; }

    if (fsys.existsSync(path))
    { return {code:409, text:'Conflict'}; }

    if (path[path.length -1] == '/')
    { fsys.mkdirSync(root +'/'+ path.substr(0, path.length -2)); }
    else
    { fsys.writeFileSync((root +'/'+ path), (data || ' '), 'utf8'); }

    return {code:200, text:'OK'};
};


http.createServer
(
    function(request, response)
    {
        let vars = url.parse(request.url);

        if (path && (path.indexOf('/') > -1) && (request.method == 'PUT'))
        {
            var resp = makePath(__dirname, vars.path, vars.data);

            response.statusCode = resp.code;
            response.setHeader('Content-Type', 'text/plain');
            response.end(resp.text);
        }
    }
).listen(8124);

Usage

You can access this from your web browser, if your server runs on the same machine, in your web-browser's address bar type: http://127.0.0.1:8124 and hit enter/return; however, please see the proper NodeJS documentation for serving the necessary client-side HTML & JavaScript as mentioned.

🌐
GitHub
github.com › Lumzdas › Peergos
GitHub - Lumzdas/Peergos: A p2p, secure file storage, social network and application protocol · GitHub
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
🌐
Peergos
book.peergos.org › features › apps.html
Custom Apps
The user approves each one individually when the App asks, so a request for a granted folder never fails for want of a permission in the manifest. ... These are already quite powerful, but we plan to add more permissions as we see more use cases. ... /peergos-api/v0/form/path.to.file – An app can POST a HTML Form and have the results stored in a file of the same name in the data folder.
🌐
GitHub
github.com › mvanhorn › Peergos
GitHub - mvanhorn/Peergos: A p2p, secure file storage, social network and application protocol · GitHub
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
🌐
GitHub
github.com › mindaula › Peergos
GitHub - mindaula/Peergos: A p2p, secure file storage, social network and application protocol · GitHub
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
Find elsewhere
🌐
GitHub
github.com › arisoik › Peergos
GitHub - arisoik/Peergos: A p2p, secure file storage, social network and application protocol · GitHub
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
Author: arisoik
🌐
DEV Community
dev.to › biplab3 › write-a-file-into-specific-folder-in-node-js-example-11kk
Write a file into specific folder in Node JS example - DEV Community
May 19, 2023 - Step 4:- Now, get the path of the specific folder where you want to write the file using the path variable with the help of the join() method as shown below.
🌐
Project Awesome
project-awesome.org › home › ipfs › peergos
Peergos — Project Awesome
The web interface is mostly coded in Java and cross compiled to JavaScript, with the exception of the Tweetnacl and scrypt libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames using a structure similar to certificate transparency. This data is mirrored on every peergos server.
🌐
GitHub
github.com › Peergos › Peergos › blob › master › README.md
Peergos/README.md at master · Peergos/Peergos · GitHub
7.0 Sharing of a file (with another user, through a secret link, or publicly) Once user A is being followed by user B, then A can share files with user B (B can revoke their following at any time) File access control is based on the cryptree system used by Wuala · A link can be generated to a file or a folder which can be shared with anyone through any medium. A link is of the form https://demo.peergos.net/#KEY_MATERIAL which has the property that even the link doesn't leak the file contents to the network, as the key material after the # is not sent to the server, but interpreted locally in the browser.
🌐
Stack Overflow
stackoverflow.com › questions › 61937031 › how-do-i-get-file-path-for-image-in-a-hidden-folder
javascript - How do I get file path for image in a hidden folder? - Stack Overflow
@rkafds dots for a relative path should be before the forward slash, not after (so instead try <img src="../img/secretImage.png">) 2020-05-21T17:02:50.89Z+00:00
🌐
GitHub
github.com › Peergos › Peergos › tree › cli
GitHub - Peergos/Peergos at cli · GitHub
The web interface is mostly coded in Java and cross compiled to Javascript, with the exception of the tweetnacl, scrypt and sha256 libraries, and a small amount of GUI code in JS for Vue.js. ... There is a pki node which ensures unique usernames. This data is mirrored on every peergos server.
🌐
Peergos
book.peergos.org › features › private-web.html
Private websites
The beautiful thing here is that ... the paths are resolved locally in the browser and all requests go through the peergos server you are connecting to. This means browser fingerprinting is irrelevant for such websites because no information can be exfiltrated! You can share websites privately with friends on peergos the same way you share any file or folder. You can even share them with anyone via a secret ...
🌐
GitHub
github.com › Peergos › Peergos › issues › 591
Change public file handler to do a redirect to a "secret" link · Issue #591 · Peergos/Peergos · GitHub
November 4, 2019 - This achieves several things: Allows us to let any user publish files and make them accessible with just a human readable path. Prevents any XSS possibility caused by letting users publish this way Gives a much prettier UI for downloadin...
Author: Peergos
Top answer
1 of 1
4

I want to write file in user directory

You cannot directly write file to user filesystem .

After searching SO for similar Questions to accurately resolve Question, was able to $ cat contents of file created at plnkr following this answer posted by @Iain at Where does PERSISTENT file system storage store with chrome? . The file was written to the user filesystem at ~/.config/[chrome, chromium] directory.

Used file manager to navigate to

  ~/.config/[chrome, chromium]/Default/File System

to review the directory; the file containing text written below, was found in a folder having two digits as a folder name, then within two further sub-directories; a sub-directory having a single lowercase letter as folder name; within this sub-directory there was another sub-directory having two digits as a folder name; the file that was written by window.webkitRequestFileSystem had eight digits as a file name, without an extension, though having correct "text/plain" MIME type; set at Blob type property.

Then at terminal

  $ cd ~/.config/[chrome, chromium]/Default/File\ System/[three digits]/[lowercase letter]/[two digits]

  $ cat [eight digits]
  Lorem Ipsum

Have not tried to create a .sh file and execute it. Would probably require placing the directory in path or moving the file to a folder in existing path; set appropriate permissions for file. Could possibly adjust this at chrome / chromium browser settings, though have not tried this either.

You could probably write a command to copy or move the file at /path/to/file to an folder in path, and execute the file; or other approach.


You can use download attribute of a element to allow download of file created by createWriter to user filesystem.

The file system is sandboxed

Because the file system is sandboxed, a web app cannot access another app's files. You also cannot read or write files to an arbitrary folder (for example, My Pictures and My Documents) on the user's hard drive.

see also at Definititons

persistent storage Persistent storage is storage that stays in the browser unless the user expunges it or the app deletes it.
temporary storage Transient storage is available to any web app. It is automatic and does not need to be requested, but the browser can delete the storage without warning.


I want to write file in user directory because it has to be user understandable.

Edit, Updated

You can use the method described above. That is, use a file manager at to review how the folders and files appear in

  ~/.config/[chrome, chromium]/Default/File System

  ## do stuff with contents of file written by `window.requestFilesystem`

To view file in chrome / chromium FileSystem you can navigate to DevTools -> Experiments -> check FileSystem inspection , log.txt should be listed at Resources tab at FileSystem .

Can also navigate to file at address bar using URL

filesystem:http://run.plnkr.co/temporary/log.txt

which should have contents

Lorem Ipsum

or

filesystem:http://run.plnkr.co/temporary/

to view all files and directories in root of temporary filesystem

See Exploring the FileSystem API

First launch chrome / chromium with --allow-file-access-from-files flag , see How do I make the Google Chrome flag "--allow-file-access-from-files" permanent? .

Next

window.requestFileSystem  = window.requestFileSystem 
                            || window.webkitRequestFileSystem;

add error handling

function errorHandler(e) {
  var msg = '';

  switch (e.message) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  console.log('Error: ' + msg);
}

You should then be able to

function writeFile(fs) {

  fs.root.getFile('log.txt', {create: true}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.onwriteend = function(e) {
        console.log('Write completed.');
        // call `readFile` here
        window.requestFileSystem(window.TEMPORARY, 1024*1024, readFile, errorHandler);

      };

      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});

      fileWriter.write(blob);

    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.TEMPORARY, 1024*1024, writeFile, errorHandler);

function readFile(fs) {

  fs.root.getFile('log.txt', {}, function(fileEntry) {

    // Get a File object representing the file,
    // then use FileReader to read its contents.
    fileEntry.file(function(file) {
       var reader = new FileReader();

       reader.onloadend = function(e) {
         console.log(e.target.result)
       };

       reader.readAsText(file);
    }, errorHandler);

  }, errorHandler);

}

plnkr http://plnkr.co/edit/EVVNYYUvHiM545T06kMC?p=preview


Note, at Chrome 38+ it is also possible to create a File object using the new File() constructor; see Chrome: Create file input from blob with Javascript? .

No, It is background process, save the data into the file exist in folder.

This approach, too, will not automatically write created file to an existing folder at user filesystem.

With either approach user action should be required to save file to user filesystem. This can be achieved using download attribute at a element , data URI How to export JavaScript array info to csv (on client side)? , or an iframe element Download File Using Javascript/jQuery ; which should prompt user to either select to save file or not save file.


See also The FileSaver interface , FileSaver.js