![]() |
install | documentation | examples | articles | changelog 16.6.0 released on Mar 08, 2023 | articles updated on Mar 20, 2023
|
vv -m
tar xvf $(vv -o)/examples/file_manager.tar.gz cd file_manager
This will create a new application home (which is "/var/lib/vv/file_manager") and do the application setup for you. Mostly that means create various subdirectories in the home folder, and assign them privileges. In this case only current user (or the result of "whoami" Linux command) will own those directories with 0700 privileges; it means a secure setup.sudo vf -i -u $(whoami) file_manager
Next, login to database db_file_manager and create the database objects for the application:echo "create user $(whoami); create database db_file_manager with owner=$(whoami); grant all on database db_file_manager to $(whoami); \q " | sudo -u postgres psql
psql -d db_file_manager -f setup.sql
The above is a standard postgres connection string that describes the login to the database you created. Since Vely uses native PostgreSQL connectivity, you can specify any connection string that your database lets you.echo "user=$(whoami) dbname=db_file_manager" > db_file_manager
Note usage of --db option to specify PostgreSQL database and the database configuration file name.vv -q --db=postgres:db_file_manager
This will start 3 daemon processes to serve the incoming requests. You can also start an adaptive server that will increase the number of processes to serve more requests, and gradually reduce the number of processes when they're not needed:vf -p 2310 -w 3 file_manager
See vf for more options to help you achieve best performance.vf -p 2310 file_manager
- SELinux and TCP portsvf -m quit file_manager
Without this step, the application will appear to not work.sudo semanage port -a -t vvport_t -p tcp 2310
sudo a2enmod proxy sudo a2enmod proxy_fcgi
For OpenSUSE:sudo vi /etc/httpd/conf/httpd.conf
Add this to the end of the file:sudo vi /etc/apache2/httpd.conf
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
sudo vi /etc/apache2/apache2.conf
sudo vi /etc/httpd/conf/httpd.conf
sudo vi /etc/apache2/httpd.conf
- Step 3:ProxyPass "/file_manager" fcgi://127.0.0.1:2310/
On Fedora systems (like RedHat) and Arch Linux:sudo systemctl restart apache2
Note: you must not have any other URL resource that starts with "/file_manager" (such as for example "/file_manager.html" or "/file_manager_something" etc.) as the web server will attempt to pass them as a reverse proxy request, and they will likely not work. If you need to, you can change the application path to be different from "/file_manager", see request_URL.sudo systemctl restart httpd
Note: if your server is on the Internet and it has a firewall, you may need to allow HTTP traffic - see ufw, firewall-cmd etc.#Start the file management application http://127.0.0.1/file_manager/start
create table if not exists files (fileName varchar(100), localPath varchar(300), extension varchar(10), description varchar(200), fileSize int, fileID bigserial primary key);
// SPDX-License-Identifier: EPL-2.0
// Copyright 2018 DaSoftver LLC.
#include "vely.h" // must always be here
// Upload and list/download files
void start ()
{
out-header default
@<h2>File Manager</h2>
@To manage the uploaded files, <a href="<<p-path>>?req=list">click here.</a><br/>
@<br/>
// Form to upload a file
@<form action="<<p-path>>" method="POST" enctype="multipart/form-data">
@ <input type="hidden" name="req" value="upload">
@ <label for="file_description">File description:</label><br>
@ <textarea name="filedesc" rows="3" columns="50"></textarea><br/>
@ <br/>
@ <label for="filename">File:</label>
@ <input type="file" name="file" value=""><br><br>
@ <input type="submit" value="Submit">
@</form>
}
// SPDX-License-Identifier: EPL-2.0
// Copyright 2018 DaSoftver LLC.
#include "vely.h" // must always be here
// Upload the file
void upload ()
{
out-header default
// file description from the upload form
input-param filedesc
// file name
input-param file_filename
// the path to uploaded file
input-param file_location
// size in bytes
input-param file_size
// the file extension
input-param file_ext
VV_UNUSED (file_ext);
@<h2>Uploading file</h2>
// insert the information about the file into the database
run-query @db_file_manager="insert into files (fileName, localPath, extension, description, fileSize) values ('%s', '%s', '%s', '%s', '%s')": file_filename, file_location, file_ext, filedesc, file_size
end-query
@File <<p-web file_filename>> of size <<p-web file_size>> is stored on server at <<p-web file_location>>. File description is <<p-web filedesc>>.<hr/>
}
// SPDX-License-Identifier: EPL-2.0
// Copyright 2018 DaSoftver LLC.
#include "vely.h" // must always be here
// List files
void list ()
{
// List current files in the database
out-header default
@<h2>List of files</h2>
@To add a file, <a href="<<p-path>>?req=start">click here</a><br/><br/>
@<table border="1">
@<tr>
@ <td>File</td><td>Description</td><td>Size</td><td>Show</td><td>Delete</td>
@</tr>
// get the list of files from the database
run-query @db_file_manager="select fileName, description, fileSize, fileID from files order by fileSize desc" output fileName, description, fileSize, fileID
query-result fileName to define file_name
query-result fileID to define file_ID
query-result description to define description
query-result fileSize to define file_size
// construct table output with links to Show and Delete files
@<tr>
@ <td><<p-web file_name>></td><td><<p-web description>><td><<p-web file_size>></td>
@ <td><a href="<<p-path>>?req=download&file_id=<<p-url file_ID>>">Show</a></td>
@ <td><a href="<<p-path>>?req=delete&action=confirm&file_id=<<p-url file_ID>>">Delete</a></td>
@</tr>
end-query
@</table>
}
// Copyright 2018 DaSoftver LLC.
#include "vely.h" // must always be here
// Download a file
void download ()
{
// Show or download a file (its ID is in the database)
input-param file_id
char *local_path=NULL;
char *ext = NULL;
// get the local path and extension of the file
run-query @db_file_manager="select localPath,extension from files where fileID='%s'" output localPath, extension : file_id row-count define num_files
query-result localPath to local_path
query-result extension to ext
end-query
// check we can find the file
if (num_files != 1) {
out-header default
@Cannot find this file!<hr/>
return;
}
// display JPG or PDF files in the browser, or download any other kind
if (!strcmp (ext, ".jpg")) {
send-file local_path headers content-type "image/jpg"
} else if (!strcmp (ext, ".pdf")) {
send-file local_path headers content-type "application/pdf"
} else {
send-file local_path headers content-type "application/octet-stream" download
}
}
// SPDX-License-Identifier: EPL-2.0
// Copyright 2018 DaSoftver LLC.
#include "vely.h" // must always be here
// Delete the file
void delete ()
{
out-header default
@<h2>Delete a file</h2>
input-param action
input-param file_id
char *file_name = NULL;
char *desc = NULL;
char *local_path = NULL;
// Get file information from the database
run-query @db_file_manager="select fileName, localPath, description from files where fileID='%s'" output fileName, localPath, description : file_id
query-result fileName to file_name
query-result description to desc
query-result localPath to local_path
end-query
if (!strcmp (action, "confirm")) { // get file information to confirm what will be deleted
@Are you sure you want to delete file <<p-web file_name>> (<<p-web desc>>)? Click <a href="<<p-path>>?req=delete&action=delete&file_id=<<p-url file_id>>">Delete</a> or click the browser's Back button to go back.<br/>
} else if (!strcmp (action, "delete")) { // actual delete file, once confirmed
begin-transaction @db_file_manager
run-query @db_file_manager= "delete from files where fileID='%s'" : file_id error define err no-loop
if (atol(err) != 0) {
@Could not delete the file (error <<p-web err>>)
rollback-transaction @db_file_manager
} else {
delete-file local_path status define st
if (st == VV_OKAY) {
commit-transaction @db_file_manager
@File deleted. Go back to <a href="<<p-path>>?req=start">start page</a>
} else {
rollback-transaction @db_file_manager
@File could not be deleted, error <<pf-web "%lld", st>>
}
}
} else {
@Unrecognized action <<p-web action>>
}
}