`Hey there.
The whole world is going crazy about this “new” AI called ChatGPT. Someone even had it programming their own vst-plugin for some audio-stuff.
So I got curious and asked ChatGPT if it would write me some fitting code for some imaginary new app where you can create individualized shares for the same folder/file for multiple users.
this is what the AI gave me. Suggested app-title Personalized Links. AI told me that this is made for an SQL-databaseusing a SHA256-Hash-algorithm.
<?php
namespace OCA\PersonalizedLinks\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IDBConnection;
class LinkController extends Controller {
private $db;
public function __construct($appName, IRequest $request, IDBConnection $db) {
parent::__construct($appName, $request);
$this->db = $db;
}
/**
* Generates a personalized link for a file and saves it in the database
* @param string $fileId The ID of the file
* @param string $username The username of the user
* @return JSONResponse
*/
public function generateLink($fileId, $username) {
$link = hash('sha256', $fileId . $username);
$query = $this->db->prepare('INSERT INTO `*PREFIX*personalized_links` (`file_id`, `username`, `link`) VALUES (?, ?, ?)');
$query->execute([$fileId, $username, $link]);
return new JSONResponse(['link' => $link]);
}
/**
* Returns the personalized link for a file
* @param string $fileId The ID of the file
* @param string $username The username of the user
* @return JSONResponse
*/
public function getLink($fileId, $username) {
$query = $this->db->prepare('SELECT `link` FROM `*PREFIX*personalized_links` WHERE `file_id` = ? AND `username` = ?');
$query->execute([$fileId, $username]);
$link = $query->fetchColumn();
return new JSONResponse(['link' => $link]);
}
}
Further on I asked for a frontend-implementation of that code and this is what ChatGPT gave me
OCA.PersonalizedLinks = {
generateLink: function(fileId) {
$.ajax({
type: 'GET',
url: OC.generateUrl('/apps/personalized_links/link/generate/' + fileId),
success: function(response) {
// Display the personalized link
console.log(response.link);
}
});
},
getLink: function(fileId) {
$.ajax({
type: 'GET',
url: OC.generateUrl('/apps/personalized_links/link/get/' + fileId),
success: function(response) {
// Display the personalized link
console.log(response.link);
}
});
}
};
I then asked the machine to make a real app from it.
To use the code described above as a Nextcloud app, you need to follow these steps:
Create a directory for your app. You can name it for example personalized_links
.
Create an info.xm
l file, in which you provide basic information about your app such as the name, version and dependencies. Here is an example for the info.xml
file
<?xml version="1.0"?>
<!--
Nextcloud app info.xml file
-->
<app>
<id>personalized_links</id>
<name>Personalized Links</name>
<description>Generate personalized links for shared files</description>
<version>1.0.0</version>
<requirements>
<php>
<minVersion>7.0.0</minVersion>
</php>
</requirements>
</app>
- create a routes.php for your API routes, e.g.
<?php
declare(strict_types=1);
/** @var \OCP\Route\IRouter $router */
$router->get(
'/apps/personalized_links/link/generate/{fileId}',
function (int $fileId) {
// Generate the personalized link here
$link = 'https://your-nextcloud.com/s/' . $fileId . '/' . uniqid();
return new \OC_OCS_Result(['link' => $link]);
}
);
$router->get(
'/apps/personalized_links/link/get/{fileId}',
function (int $fileId) {
// Retrieve the personalized link here
$link = 'https://your-nextcloud.com/s/' . $fileId . '/' . uniqid();
return new \OC_OCS_Result(['link' => $link]);
}
);
- Create a
js
file for your frontend code. Here is an example for thepersonalized_links.js
file:
OCA.PersonalizedLinks = {
generateLink: function(fileId) {
$.ajax({
type: 'GET',
url: OC.generateUrl('/apps/personalized_links/link/generate/' + fileId),
success: function(response) {
// Display the personalized link
console.log(response.link);
}
});
},
getLink: function(fileId) {
$.ajax({
type: 'GET',
So how is that? What do you think? Would it work like that?