paint-brush
Web faýl dolandyryjysyny gurmak üçin köp kod gerek dältarapyndan@gliimlang
Täze taryh

Web faýl dolandyryjysyny gurmak üçin köp kod gerek däl

tarapyndan Gliimly7m2024/12/17
Read on Terminal Reader

Örän uzyn; Okamak

100 setirden az kodda peýdaly web programmasyny guruň.
featured image - Web faýl dolandyryjysyny gurmak üçin köp kod gerek däl
Gliimly HackerNoon profile picture

Web brauzerine faýllary ýüklemek we göçürip almak islendik web programmasynda ýa-da hyzmatynda diýen ýaly umumy mesele. Bu makala, muny gaty az kodlamak bilen - 100 setirden az kod bilen nädip etmelidigini görkezýär. Ulanylýan maglumatlar binýady PostgreSQL, web serweri bolsa Nginx.

“Gliimly” -ny programma serweri we programmirleme dili hökmünde ulanarsyňyz. Öndürijilik we howpsuzlyk, şeýle hem has baý web işlemegi üçin web serweriniň aňyrsynda işlär. Şeýlelik bilen, ahyrky ulanyjy programma serweriňiz bilen göni gürleşip bilmeýär, sebäbi şeýle haýyşlaryň hemmesi web serwerinden geçýär, arka programmaňyz has gowy işlemek üçin göni programma serweriňiz bilen gürleşip biler.

Häzirki wagtda giren Linux ulanyjynyňyzyň bu programma eýe boljakdygyny, deslapky kod katalogyny döredjekdigini we "faýl dolandyryjysy" atly Gliimly programmasyny döredjekdigini göz öňünde tutuň:


 mkdir filemgr cd filemgr gg -k file-manager


Ondan soň, häzirki hasaba giren ulanyja (ýagny parolsyz gurnama) eýe bolan "db_file_manager" atly PostgreSQL maglumat bazasyny dörediň:

 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


Maglumatlar bazasyny suratlandyrýan Gliimly tarapyndan ulanylýan maglumat bazasynyň konfigurasiýa faýly dörediň (bu "db" faýly):

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


Häzirki wagtda serwerde saklanýan faýllary saklaýan SQL tablisasyny dörediň:

 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


Netijede, çeşme Gliimly faýllaryny dörediň. Ilki bilen "start.gliim" faýly dörediň we göçüriň we göçüriň:

 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


"List.gliim" faýly dörediň we göçüriň:

 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


"Upload.gliim" faýly dörediň we göçüriň:

 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


"Download.gliim" faýly dörediň we göçüriň:

 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


"Delete.gliim" faýly dörediň we göçüriň:

 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


Arza beriň:

 gg -q --db=postgres:db


Programma serweriňizi işlediň:

 mgrg file-manager


Web serwer Gliimly programma serweriniň öňünde oturýar, şonuň üçin ony gurnamaly. Bu mysal Ubuntu üçin, şonuň üçin Nginx konfigurasiýa faýlyny redaktirläň:

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


Muny "serwer {}" bölümine goşuň ("client_max_body_size" adaty ululykdaky suratlary ýüklemäge mümkinçilik berýär):

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


Nginx-i täzeden açyň:

 sudo systemctl restart nginx


Web brauzeriňize giriň we giriň:

 http://127.0.0.1/file-manager/start


Ahyrky netijäniň görnüşi şu. Elbetde, diňe ýalaňaç süňkli HTML ulanýardyk, ýöne bu ýerde asla many ýok. Islendik öňdebaryjy tehnologiýany ulanyp bilersiňiz, esasy zat Gliimly-ny web programmalary / hyzmatlary üçin arka serwer hökmünde görkezmek.

Ine, baş faýl, faýl ýüklemek formasy we liFilest faýllaryna baglanyşyk:

Faýl dolandyryjysynyň baş sahypasy


Faýllaryň sanawy:

Sanaw faýly


Faýly pozmagy haýyş etmek:

Öçürmegi tassyklamagy soraň


Öçürmegiň tassyklamasy:

Faýly pozuň