| 801 | } |
| 802 | |
| 803 | void CrossPointWebServer::handleRename() const { |
| 804 | if (!server->hasArg("path") || !server->hasArg("name")) { |
| 805 | server->send(400, "text/plain", "Missing path or new name"); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | String itemPath = normalizeWebPath(server->arg("path")); |
| 810 | String newName = server->arg("name"); |
| 811 | newName.trim(); |
| 812 | |
| 813 | if (itemPath.isEmpty() || itemPath == "/") { |
| 814 | server->send(400, "text/plain", "Invalid path"); |
| 815 | return; |
| 816 | } |
| 817 | if (newName.isEmpty()) { |
| 818 | server->send(400, "text/plain", "New name cannot be empty"); |
| 819 | return; |
| 820 | } |
| 821 | if (newName.indexOf('/') >= 0 || newName.indexOf('\\') >= 0) { |
| 822 | server->send(400, "text/plain", "Invalid file name"); |
| 823 | return; |
| 824 | } |
| 825 | if (isProtectedItemName(newName)) { |
| 826 | server->send(403, "text/plain", "Cannot rename to protected name"); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | const String itemName = itemPath.substring(itemPath.lastIndexOf('/') + 1); |
| 831 | if (isProtectedItemName(itemName)) { |
| 832 | server->send(403, "text/plain", "Cannot rename protected item"); |
| 833 | return; |
| 834 | } |
| 835 | if (newName == itemName) { |
| 836 | server->send(200, "text/plain", "Name unchanged"); |
| 837 | return; |
| 838 | } |
| 839 | |
| 840 | if (!Storage.exists(itemPath.c_str())) { |
| 841 | server->send(404, "text/plain", "Item not found"); |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | HalFile file = Storage.open(itemPath.c_str()); |
| 846 | if (!file) { |
| 847 | server->send(500, "text/plain", "Failed to open file"); |
| 848 | return; |
| 849 | } |
| 850 | if (file.isDirectory()) { |
| 851 | file.close(); |
| 852 | server->send(400, "text/plain", "Only files can be renamed"); |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | String parentPath = itemPath.substring(0, itemPath.lastIndexOf('/')); |
| 857 | if (parentPath.isEmpty()) { |
| 858 | parentPath = "/"; |
| 859 | } |
| 860 | String newPath = parentPath; |
nothing calls this directly
no test coverage detected