paint-brush
Ou pa bezwen anpil kòd pou konstwi yon Web File Managerpa@gliimlang
Nouvo istwa

Ou pa bezwen anpil kòd pou konstwi yon Web File Manager

pa Gliimly7m2024/12/17
Read on Terminal Reader

Twò lontan; Pou li

Bati aplikasyon entènèt itil nan mwens pase 100 liy kòd.
featured image - Ou pa bezwen anpil kòd pou konstwi yon Web File Manager
Gliimly HackerNoon profile picture

Telechaje ak telechaje fichye yo nan navigatè entènèt se yon travay komen nan prèske nenpòt aplikasyon oswa sèvis entènèt. Atik sa a montre kijan pou fè sa ak yon ti kodaj - nan mwens pase 100 liy kòd. Baz done yo itilize se PostgreSQL, ak sèvè entènèt la se Nginx.

Ou pral sèvi ak Gliimly kòm yon sèvè aplikasyon ak langaj pwogramasyon an. Li pral kouri dèyè sèvè entènèt la pou pèfòmans ak sekirite, osi byen ke pou pèmèt fonksyonalite entènèt ki pi rich. Konsa, itilizatè final la pa ka pale ak sèvè aplikasyon w lan dirèkteman paske tout demann sa yo ale nan sèvè entènèt la, pandan y ap aplikasyon back-end ou a ka pale dirèkteman ak sèvè aplikasyon w lan pou pi bon pèfòmans.

Sipoze itilizatè Linux ki konekte kounye a ou pral posede aplikasyon an, kreye yon anyè kòd sous epi tou li kreye aplikasyon Gliimly ki rele "file-manager":


 mkdir filemgr cd filemgr gg -k file-manager


Apre sa, kreye baz done PostgreSQL ki rele "db_file_manager", ki posede pa itilizatè ki konekte kounye a (sa vle di konfigirasyon san modpas):

 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


Kreye yon fichye konfigirasyon baz done itilize pa Gliimly ki dekri baz done a (li se yon fichye "db"):

 echo "user=$(whoami) dbname=db_file_manager" > db


Kreye tab SQL ki pral kenbe dosye ki estoke kounye a sou sèvè a:

 echo "create table if not exists files (fileName varchar(100), localPath varchar(300), extension varchar(10), description varchar(200), fileSize int, fileID bigserial primary key);" | psql -d db_file_manager


Finalman, kreye sous Gliimly dosye. Premye kreye "start.gliim" fichye epi kopye epi kole:

 begin-handler /start public @<h2>File Manager</h2> @To manage the uploaded files, <a href="<<p-path "/list">>">click here.</a><br/> @<br/> @<form action="<<p-path "/upload">>" method="POST" enctype="multipart/form-data"> @ <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> end-handler


Kreye fichye "list.gliim" epi kopye epi kole:

 begin-handler /list public @<h2>List of files</h2> @To add a file, <a href="<<p-path "/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> run-query @db= \ "select fileName, description, fileSize, fileID from files order by fileSize desc" \ output file_name, description noencode, file_size, file_ID @<tr> @ <td><<p-web file_name>></td><td><<p-web description>><td><<p-web file_size>></td> @ <td><a href="<<p-path "/download">>/file_id=<<p-url file_ID>>">Show</a></td> @ <td><a href="<<p-path "/delete">>/action=confirm/file_id=<<p-url file_ID>>">Delete</a></td> @</tr> end-query @</table> end-handler


Kreye fichye "upload.gliim" epi kopye epi kole:

 begin-handler /upload public get-param filedesc // file description from the upload form get-param file_filename // file name get-param file_location // the path to uploaded file get-param file_size // size in bytes get-param file_ext // the file extension @<h2>Uploading file</h2> run-query @db= \ "insert into files (fileName, localPath, extension, description, fileSize) \ values ('%s', '%s', '%s', '%s', '%s')" \ input 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/> end-handler


Kreye dosye "download.gliim" epi kopye epi kole:

 begin-handler /download public get-param file_id run-query @db= \ "select localPath,extension from files where fileID='%s'" \ input file_id \ output local_path, ext \ row-count num_files if-true ext equal ".jpg" send-file local_path headers content-type "image/jpg" else-if ext equal ".pdf" send-file local_path headers content-type "application/pdf" else-if send-file local_path headers content-type "application/octet-stream" download end-if end-query if-true num_files not-equal 1 @Cannot find this file!<hr/> exit-handler end-if end-handler


Kreye fichye "delete.gliim" epi kopye epi kole:

 begin-handler /delete public @<h2>Delete a file</h2> get-param action get-param file_id run-query @db="select fileName, localPath, description from files where fileID='%s'" output file_name, local_path, desc input file_id if-true action equal "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 "/delete">>?action=delete&amp;file_id=<<p-url file_id>>">Delete</a> or click the browser's Back button to go back.<br/> else-if action equal "delete" // actual delete file, once confirmed begin-transaction @db run-query @db= "delete from files where fileID='%s'" input file_id error err no-loop if-true err not-equal "0" @Could not delete the file (error <<p-web err>>) rollback-transaction @db else-if delete-file local_path status st if-true st equal GG_OKAY commit-transaction @db @File deleted. Go back to <a href="<<p-path "/start">>">start page</a> else-if rollback-transaction @db @File could not be deleted, error <<p-num st>> end-if end-if else-if @Unrecognized action <<p-web action>> end-if end-query end-handler


Fè aplikasyon an:

 gg -q --db=postgres:db


Kouri sèvè aplikasyon w lan:

 mgrg file-manager


Yon sèvè entènèt chita devan sèvè aplikasyon Gliimly, kidonk li bezwen konfigirasyon. Egzanp sa a se pou Ubuntu, kidonk edite fichye konfigirasyon Nginx la:

 sudo vi /etc/nginx/sites-enabled/default


Ajoute sa a nan seksyon "sèvè {}" ("client_max_body_size" pèmèt yo telechaje imaj ki gen gwosè tipik):

 location /file-manager/ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:///var/lib/gg/file-manager/sock/sock; } client_max_body_size 100M;


Rekòmanse Nginx:

 sudo systemctl restart nginx


Ale nan navigatè entènèt ou a, epi antre:

 http://127.0.0.1/file-manager/start


Sa a se sa rezilta final la sanble. Evidamman, nou te itilize jis HTML bare-bone, men sa a pa pwen isit la ditou. Ou ka itilize nenpòt kalite teknoloji front-end, pwen an se demontre Gliimly kòm yon sèvè back-end pou aplikasyon / sèvis entènèt.

Men ekran lakay la, ak fòm pou telechaje yon fichye ak yon lyen ki mennen nan liFilest nan fichye yo:

Paj lakay manadjè dosye


Lis fichye yo:

Lis dosye


Mande pou efase yon fichye:

Mande konfimasyon pou efase


Konfimasyon efase:

Efase fichye a