Looking for guidance to add a functional "delete" link to delete file(s) in a folder

Continuing the discussion from Unable to download files from the browser:

Hi everyone, i'm a newbiew to NodeRed and came across this post which is able to create a list from a folder and download link. it works great!

had tried to understand the codes and been playing around with it, however still unable to let the "delete" link to be functional.

image

<table>
    <th>Files found</th>
    
    {{#files}}
     <tr>
        <td> {{.}} </td>
        <td> <a href="/files/{{{.}}}">download</a>
        <td>
        <td> <a href="/delete/{{{.}}}">delete</a>
        <td>
    </tr>
    {{/files}}
</table>

did as above but lost of direction what can i do next to move on. had lots of trial and error to have more understanding but however what i get most of the time was as below. on the positive side was the filename returned (although with error) was the name intended to be deleted :joy:

"Cannot GET /delete/20220411.csv"

Thanks in advance! :blush:

Did you add an endpoint for /delete & hook that up to the necessary logic to perform the delete?

Hi Steve! thanks for the prompt response

[
    {
        "id": "0829d2663c2cbbca",
        "type": "function",
        "z": "e582dbe7569c9c4e",
        "name": "Set base path",
        "func": "//restrict to c:\\temp\\\nvar basePath = flow.get(\"basePath\");\nvar filename = msg.req.params.fn;\n\n\nif(filename.includes(\"..\\\\\")){\n    msg.payload = \"Illegal file path\";\n    msg.statusCode = 405;//not allowed\n    return [null, msg];//fire output 2\n} else if(filename.includes(\"../\")){\n    msg.payload = \"Illegal file path\";\n    msg.statusCode = 405;//not allowed\n    return [null, msg];//fire output 2\n} \n//TODO: add more checks\n\nmsg.filename = basePath + filename;\nreturn [msg, null];//fire output 1\n\n\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1100,
        "y": 660,
        "wires": [
            [
                "11ed99d831349ff6",
                "6a8dcab2d3b975be"
            ],
            [
                "b2f7eb8cfcba09d8"
            ]
        ]
    },
    {
        "id": "b2f7eb8cfcba09d8",
        "type": "http response",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 1410,
        "y": 720,
        "wires": []
    },
    {
        "id": "11ed99d831349ff6",
        "type": "file in",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "filename": "",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 1320,
        "y": 660,
        "wires": [
            [
                "b2f7eb8cfcba09d8"
            ]
        ]
    },
    {
        "id": "740a73135bb53bb7",
        "type": "catch",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "scope": null,
        "uncaught": false,
        "x": 880,
        "y": 720,
        "wires": [
            [
                "df88955353671807",
                "bcbc4da718e60095"
            ]
        ]
    },
    {
        "id": "df88955353671807",
        "type": "function",
        "z": "e582dbe7569c9c4e",
        "name": "Set 404",
        "func": "msg.payload = msg.error;\nmsg.statusCode = 404;//resource not found\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1200,
        "y": 720,
        "wires": [
            [
                "b2f7eb8cfcba09d8"
            ]
        ]
    },
    {
        "id": "bcbc4da718e60095",
        "type": "debug",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1050,
        "y": 700,
        "wires": []
    },
    {
        "id": "5ffcf157d5c4e561",
        "type": "ui_template",
        "z": "e582dbe7569c9c4e",
        "group": "dfb4a60f.d788f8",
        "name": "ui_temlplate - present download links on dashboard",
        "order": 1,
        "width": "7",
        "height": "8",
        "format": "<div ng-bind-html=\"msg.payload\"></div>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": false,
        "templateScope": "local",
        "className": "",
        "x": 1310,
        "y": 800,
        "wires": [
            []
        ]
    },
    {
        "id": "5429470c045925c9",
        "type": "comment",
        "z": "e582dbe7569c9c4e",
        "name": "Create http endpoint <nodered>/files/xxx  where xxx is the file name to download",
        "info": "",
        "x": 1040,
        "y": 620,
        "wires": []
    },
    {
        "id": "f51f310fa49f72e3",
        "type": "http in",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "url": "/files/:fn",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 890,
        "y": 660,
        "wires": [
            [
                "0829d2663c2cbbca",
                "86e4b4968b4f185b"
            ]
        ]
    },
    {
        "id": "cede760ddd3693af",
        "type": "inject",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "/home/orangepi/datalog/",
        "payloadType": "str",
        "x": 890,
        "y": 580,
        "wires": [
            [
                "db7cbbbe1ea13b1a"
            ]
        ]
    },
    {
        "id": "db7cbbbe1ea13b1a",
        "type": "change",
        "z": "e582dbe7569c9c4e",
        "name": "store flow.basePath",
        "rules": [
            {
                "t": "set",
                "p": "basePath",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1130,
        "y": 580,
        "wires": [
            []
        ]
    },
    {
        "id": "80825c1d79c2b94c",
        "type": "ui_ui_control",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "events": "all",
        "x": 840,
        "y": 800,
        "wires": [
            [
                "c9c91dfef1e3aec8"
            ]
        ]
    },
    {
        "id": "0e39a125e27ab6c6",
        "type": "comment",
        "z": "e582dbe7569c9c4e",
        "name": "Setup basePath",
        "info": "",
        "x": 840,
        "y": 540,
        "wires": []
    },
    {
        "id": "1e4aca0214eb0cc6",
        "type": "comment",
        "z": "e582dbe7569c9c4e",
        "name": "Get files in basePath and generate table",
        "info": "",
        "x": 910,
        "y": 760,
        "wires": []
    },
    {
        "id": "ae8f44c9ddf01a9c",
        "type": "inject",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 860,
        "y": 840,
        "wires": [
            [
                "c9c91dfef1e3aec8"
            ]
        ]
    },
    {
        "id": "d680804e3853c21b",
        "type": "template",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<table>\n    <th>Files</th>\n    \n    {{#files}}\n     <tr>\n        <td> {{.}} </td>\n        <td> <a href=\"/files/{{{.}}}\">download</a>\n        <td>\n        <td> <a href=\"/delete/{{{.}}}\">delete</a>\n        <td>\n    </tr>\n    {{/files}}\n</table>",
        "output": "str",
        "x": 1040,
        "y": 840,
        "wires": [
            [
                "5ffcf157d5c4e561"
            ]
        ]
    },
    {
        "id": "c9c91dfef1e3aec8",
        "type": "fs-ops-dir",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "path": "basePath",
        "pathType": "flow",
        "filter": "*.csv",
        "filterType": "str",
        "dir": "files",
        "dirType": "msg",
        "x": 1000,
        "y": 800,
        "wires": [
            [
                "d680804e3853c21b"
            ]
        ]
    },
    {
        "id": "86e4b4968b4f185b",
        "type": "debug",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1130,
        "y": 920,
        "wires": []
    },
    {
        "id": "b20368ff6c391b0a",
        "type": "http in",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "url": "/delete/:fn",
        "method": "delete",
        "upload": false,
        "swaggerDoc": "",
        "x": 870,
        "y": 960,
        "wires": [
            [
                "0829d2663c2cbbca"
            ]
        ]
    },
    {
        "id": "6a8dcab2d3b975be",
        "type": "fs-ops-delete",
        "z": "e582dbe7569c9c4e",
        "name": "",
        "path": "",
        "pathType": "str",
        "filename": "*",
        "filenameType": "msg",
        "x": 1330,
        "y": 940,
        "wires": [
            [
                "b2f7eb8cfcba09d8"
            ]
        ]
    },
    {
        "id": "dfb4a60f.d788f8",
        "type": "ui_group",
        "name": "Select files to download",
        "tab": "f2e6c77a41682d29",
        "order": 2,
        "disp": true,
        "width": "7",
        "collapse": false,
        "className": ""
    },
    {
        "id": "f2e6c77a41682d29",
        "type": "ui_tab",
        "name": "Page 2: Data",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

i had edited the template and simply added the http request node as above. i'm not sure if i missed out anything else.

added nodes are

  1. another http request node with delete
  2. fs-ops-delete node

do you mind to have a look at it?

thanks in advance!!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.