Version 21.35: Oct 18, 2021 New module swad_file_database for database queries related to files.

This commit is contained in:
acanas 2021-10-18 20:49:14 +02:00
parent e9607cb177
commit 0f8f8fc396
10 changed files with 287 additions and 174 deletions

View File

@ -48,9 +48,9 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \
swad_exam_database.o swad_exam_log.o swad_exam_print.o \
swad_exam_result.o swad_exam_session.o swad_exam_set.o \
swad_figure.o swad_figure_cache.o swad_figure_database.o swad_file.o \
swad_file_extension.o swad_file_MIME.o swad_firewall.o \
swad_firewall_database.o swad_follow.o swad_follow_database.o \
swad_form.o swad_forum.o swad_forum_database.o \
swad_file_database.o swad_file_extension.o swad_file_MIME.o \
swad_firewall.o swad_firewall_database.o swad_follow.o \
swad_follow_database.o swad_form.o swad_forum.o swad_forum_database.o \
swad_game.o swad_game_database.o swad_global.o swad_group.o \
swad_group_database.o swad_help.o swad_hierarchy.o \
swad_hierarchy_config.o swad_hierarchy_database.o swad_holiday.o \

View File

@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par
TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo.
*/
#define Log_PLATFORM_VERSION "SWAD 21.34 (2021-10-15)"
#define Log_PLATFORM_VERSION "SWAD 21.35 (2021-10-18)"
#define CSS_FILE "swad20.45.css"
#define JS_FILE "swad20.69.1.js"
/*
TODO: Rename CENTRE to CENTER in help wiki.
TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams
Version 21.35: Oct 18, 2021 New module swad_file_database for database queries related to files. (320171 lines)
Version 21.34: Oct 15, 2021 New module swad_session_database for database queries related to sessions. (320081 lines)
Version 21.33.2: Oct 15, 2021 Fixed bug getting name of degree. (319950 lines)
Version 21.33.1: Oct 15, 2021 Number of users/courses to show big-list warning increased from 500 to 1000. (319944 lines)

View File

@ -387,7 +387,7 @@ mysql> DESCRIBE brw_caches;
3 rows in set (0.01 sec)
*/
DB_CreateTable ("CREATE TABLE IF NOT EXISTS brw_caches ("
"SessionId CHAR(43) NOT NULL," // Cns_BYTES_SESSION_ID
"SessionId CHAR(43) NOT NULL," // Cns_BYTES_SESSION_ID
"PrivPath VARCHAR(4096) COLLATE latin1_bin NOT NULL," // PATH_MAX
"TmpPubDir VARCHAR(4096) COLLATE latin1_bin NOT NULL," // PATH_MAX
"UNIQUE INDEX(SessionId))");

View File

@ -42,6 +42,7 @@
#include "swad_error.h"
#include "swad_global.h"
#include "swad_file.h"
#include "swad_file_database.h"
#include "swad_string.h"
/*****************************************************************************/
@ -637,3 +638,54 @@ void Fil_WriteFileSizeFull (double SizeInBytes,
snprintf (FileSizeStr,Fil_MAX_BYTES_FILE_SIZE_STRING + 1,"%.1f TiB",
SizeInBytes / Ti);
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Fil_AddPublicDirToCache (const char *FullPathPriv,
const char TmpPubDir[PATH_MAX + 1])
{
if (Gbl.Session.IsOpen)
{
/* Delete possible old entry */
Fil_DB_RemovePublicDirFromCache (FullPathPriv);
/* Insert new entry */
Fil_DB_AddPublicDirToCache (FullPathPriv,TmpPubDir);
}
}
/*****************************************************************************/
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
bool Fil_GetPublicDirFromCache (const char *FullPathPriv,
char TmpPubDir[PATH_MAX + 1])
{
bool Cached;
bool TmpPubDirExists;
/***** Reset temporary directory *****/
TmpPubDir[0] = '\0';
if (Gbl.Session.IsOpen)
{
/***** Get temporary directory from cache *****/
Fil_DB_GetPublicDirFromCache (FullPathPriv,TmpPubDir);
Cached = (TmpPubDir[0] != '\0');
/***** Check if temporary public directory exists *****/
if (Cached)
{
/* If not exists (it could be deleted if its lifetime has expired)
==> remove from cache */
TmpPubDirExists = Fil_CheckIfPathExists (TmpPubDir);
if (!TmpPubDirExists)
Fil_DB_RemovePublicDirFromCache (FullPathPriv);
return TmpPubDirExists;
}
}
return false;
}

View File

@ -93,4 +93,11 @@ void Fil_WriteFileSizeBrief (double SizeInBytes,
void Fil_WriteFileSizeFull (double SizeInBytes,
char FileSizeStr[Fil_MAX_BYTES_FILE_SIZE_STRING + 1]);
//------------------------------- File caches ---------------------------------
void Fil_AddPublicDirToCache (const char *FullPathPriv,
const char TmpPubDir[PATH_MAX + 1]);
bool Fil_GetPublicDirFromCache (const char *FullPathPriv,
char TmpPubDir[PATH_MAX + 1]);
#endif

142
swad_file_database.c Normal file
View File

@ -0,0 +1,142 @@
// swad_file_database.c: files, operations with database
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2021 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
// #include <ctype.h> // For isprint, isspace, etc.
// #include <dirent.h> // For scandir, etc.
// #include <errno.h> // For errno
// #include <linux/limits.h> // For PATH_MAX
// #include <stddef.h> // For NULL
// #include <stdio.h> // For FILE,fprintf
// #include <stdlib.h> // For exit, system, free, etc.
// #include <string.h> // For string functions
// #include <sys/stat.h> // For mkdir
// #include <sys/types.h> // For mkdir
// #include <unistd.h> // For unlink
// #include "swad_config.h"
#include "swad_database.h"
// #include "swad_error.h"
#include "swad_global.h"
#include "swad_file_database.h"
// #include "swad_string.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Fil_DB_AddPublicDirToCache (const char *FullPathPriv,
const char TmpPubDir[PATH_MAX + 1])
{
DB_QueryINSERT ("can not cache file",
"INSERT INTO brw_caches"
" (SessionId,PrivPath,TmpPubDir)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
FullPathPriv,
TmpPubDir);
}
/*****************************************************************************/
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
void Fil_DB_GetPublicDirFromCache (const char *FullPathPriv,
char TmpPubDir[PATH_MAX + 1])
{
DB_QuerySELECTString (TmpPubDir,PATH_MAX,"can not get check if file is cached",
"SELECT TmpPubDir"
" FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,
FullPathPriv);
}
/*****************************************************************************/
/******** Remove public directory used to link private path to cache *********/
/*****************************************************************************/
void Fil_DB_RemovePublicDirFromCache (const char *FullPathPriv)
{
if (Gbl.Session.IsOpen)
DB_QueryDELETE ("can not remove cached file",
"DELETE FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,
FullPathPriv);
}
/*****************************************************************************/
/****** Remove public directories used to link private paths from cache ******/
/*****************************************************************************/
void Fil_DB_RemovePublicDirsCache (void)
{
if (Gbl.Session.IsOpen)
DB_QueryDELETE ("can not cache file",
"DELETE FROM brw_caches"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/****** Remove public directories used to link private paths from cache ******/
/****** (from expired sessions) ******/
/*****************************************************************************/
void Fil_DB_RemovePublicDirsFromExpiredSessions (void)
{
DB_QueryDELETE ("can not remove public directories in expired sessions",
"DELETE FROM brw_caches"
" WHERE SessionId NOT IN"
" (SELECT SessionId"
" FROM ses_sessions)");
}

52
swad_file_database.h Normal file
View File

@ -0,0 +1,52 @@
// swad_file_database.h: files, operations with database
#ifndef _SWAD_FIL_DB
#define _SWAD_FIL_DB
/*
SWAD (Shared Workspace At a Distance in Spanish),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2021 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#include <linux/limits.h> // For PATH_MAX
// #include <stdbool.h> // For boolean type
// #include <stdio.h> // For FILE
// #include <time.h> // For time_t
/*****************************************************************************/
/************************** Public types and constants ***********************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Public prototypes *****************************/
/*****************************************************************************/
//------------------------------- File caches ---------------------------------
void Fil_DB_AddPublicDirToCache (const char *FullPathPriv,
const char TmpPubDir[PATH_MAX + 1]);
void Fil_DB_GetPublicDirFromCache (const char *FullPathPriv,
char TmpPubDir[PATH_MAX + 1]);
void Fil_DB_RemovePublicDirFromCache (const char *FullPathPriv);
void Fil_DB_RemovePublicDirsCache (void);
void Fil_DB_RemovePublicDirsFromExpiredSessions (void);
#endif

View File

@ -1506,7 +1506,7 @@ static void Med_ShowJPG (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathJPGPriv))
{
/***** Get cached public link to private file *****/
Cached = Brw_GetPublicDirFromCache (FullPathJPGPriv,TmpPubDir);
Cached = Fil_GetPublicDirFromCache (FullPathJPGPriv,TmpPubDir);
if (!Cached)
{
@ -1517,7 +1517,7 @@ static void Med_ShowJPG (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Brw_AddPublicDirToCache (FullPathJPGPriv,TmpPubDir);
Fil_AddPublicDirToCache (FullPathJPGPriv,TmpPubDir);
}
/***** Show media *****/
@ -1570,7 +1570,7 @@ static void Med_ShowGIF (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathGIFPriv)) // The animated GIF image
{
/***** Get cached public link to private file *****/
Cached = Brw_GetPublicDirFromCache (FullPathGIFPriv,TmpPubDir);
Cached = Fil_GetPublicDirFromCache (FullPathGIFPriv,TmpPubDir);
if (!Cached)
{
@ -1582,7 +1582,7 @@ static void Med_ShowGIF (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Brw_AddPublicDirToCache (FullPathGIFPriv,TmpPubDir);
Fil_AddPublicDirToCache (FullPathGIFPriv,TmpPubDir);
}
/***** Create URLs pointing to symbolic links *****/
@ -1658,7 +1658,7 @@ static void Med_ShowVideo (const struct Med_Media *Media,
if (Fil_CheckIfPathExists (FullPathVideoPriv))
{
/***** Get cached public link to private file *****/
Cached = Brw_GetPublicDirFromCache (FullPathVideoPriv,TmpPubDir);
Cached = Fil_GetPublicDirFromCache (FullPathVideoPriv,TmpPubDir);
if (!Cached)
{
@ -1669,7 +1669,7 @@ static void Med_ShowVideo (const struct Med_Media *Media,
snprintf (TmpPubDir,sizeof (TmpPubDir),"%s/%s",
Gbl.FileBrowser.TmpPubDir.L,Gbl.FileBrowser.TmpPubDir.R);
Brw_AddPublicDirToCache (FullPathVideoPriv,TmpPubDir);
Fil_AddPublicDirToCache (FullPathVideoPriv,TmpPubDir);
}
/***** Create URL pointing to symbolic link *****/
@ -1967,8 +1967,8 @@ void Med_RemoveMediaFromAllRows (unsigned NumMedia,MYSQL_RES *mysql_res)
void Med_RemoveMedia (long MedCod)
{
char PathMedPriv[PATH_MAX + 1];
char *FullPathMediaPriv;
char PathPriv[PATH_MAX + 1];
char *FullPathPriv;
struct Med_Media Media;
/***** Trivial case *****/
@ -1993,7 +1993,7 @@ void Med_RemoveMedia (long MedCod)
if (Media.Name[0])
{
/***** Build path to private directory with the media *****/
snprintf (PathMedPriv,sizeof (PathMedPriv),"%s/%c%c",
snprintf (PathPriv,sizeof (PathPriv),"%s/%c%c",
Cfg_PATH_MEDIA_PRIVATE,
Media.Name[0],
Media.Name[1]);
@ -2003,37 +2003,37 @@ void Med_RemoveMedia (long MedCod)
{
case Med_JPG:
/***** Remove private JPG file *****/
if (asprintf (&FullPathMediaPriv,"%s/%s.%s",
PathMedPriv,Media.Name,Med_Extensions[Med_JPG]) < 0)
if (asprintf (&FullPathPriv,"%s/%s.%s",
PathPriv,Media.Name,Med_Extensions[Med_JPG]) < 0)
Err_NotEnoughMemoryExit ();
unlink (FullPathMediaPriv);
free (FullPathMediaPriv);
unlink (FullPathPriv);
free (FullPathPriv);
break;
case Med_GIF:
/***** Remove private GIF file *****/
if (asprintf (&FullPathMediaPriv,"%s/%s.%s",
PathMedPriv,Media.Name,Med_Extensions[Med_GIF]) < 0)
if (asprintf (&FullPathPriv,"%s/%s.%s",
PathPriv,Media.Name,Med_Extensions[Med_GIF]) < 0)
Err_NotEnoughMemoryExit ();
unlink (FullPathMediaPriv);
free (FullPathMediaPriv);
unlink (FullPathPriv);
free (FullPathPriv);
/***** Remove private PNG file *****/
if (asprintf (&FullPathMediaPriv,"%s/%s.png",
PathMedPriv,Media.Name) < 0)
if (asprintf (&FullPathPriv,"%s/%s.png",
PathPriv,Media.Name) < 0)
Err_NotEnoughMemoryExit ();
unlink (FullPathMediaPriv);
free (FullPathMediaPriv);
unlink (FullPathPriv);
free (FullPathPriv);
break;
case Med_MP4:
case Med_WEBM:
case Med_OGG:
/***** Remove private video file *****/
if (asprintf (&FullPathMediaPriv,"%s/%s.%s",
PathMedPriv,Media.Name,Med_Extensions[Media.Type]) < 0)
if (asprintf (&FullPathPriv,"%s/%s.%s",
PathPriv,Media.Name,Med_Extensions[Media.Type]) < 0)
Err_NotEnoughMemoryExit ();
unlink (FullPathMediaPriv);
free (FullPathMediaPriv);
unlink (FullPathPriv);
free (FullPathPriv);
break;
default:

View File

@ -33,6 +33,7 @@
#include "swad_connected_database.h"
#include "swad_database.h"
#include "swad_error.h"
#include "swad_file_database.h"
#include "swad_global.h"
#include "swad_pagination.h"
#include "swad_parameter.h"
@ -57,8 +58,6 @@ static void Ses_RemoveSessionFromDB (void);
static bool Ses_CheckIfParamIsAlreadyInDB (const char *ParamName);
static void Brw_DB_DeletePublicDirFromCache (const char *FullPathMediaPriv);
/*****************************************************************************/
/************************** Get number of open sessions **********************/
/*****************************************************************************/
@ -108,7 +107,7 @@ void Ses_CloseSession (void)
if (Gbl.Usrs.Me.Logged)
{
/***** Remove links to private files from cache *****/
Brw_DB_RemovePublicDirsCache ();
Fil_DB_RemovePublicDirsCache ();
/***** Remove session from database *****/
Ses_RemoveSessionFromDB ();
@ -121,7 +120,7 @@ void Ses_CloseSession (void)
/***** Remove unused data associated to expired sessions *****/
Ses_RemoveParamsFromExpiredSessions ();
Brw_DB_RemovePublicDirsFromExpiredSessions ();
Fil_DB_RemovePublicDirsFromExpiredSessions ();
/***** Now, user is not logged in *****/
Gbl.Usrs.Me.Role.LoggedBeforeCloseSession = Gbl.Usrs.Me.Role.Logged;
@ -301,132 +300,3 @@ void Ses_GetParamFromDB (const char *ParamName,char *ParamValue,size_t StrSize)
Gbl.Session.Id,
ParamName);
}
/*****************************************************************************/
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
bool Brw_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1])
{
bool Cached;
bool TmpPubDirExists;
/***** Reset temporary directory *****/
TmpPubDir[0] = '\0';
if (Gbl.Session.IsOpen)
{
/***** Get temporary directory from cache *****/
Brw_DB_GetPublicDirFromCache (FullPathMediaPriv,TmpPubDir);
Cached = (TmpPubDir[0] != '\0');
/***** Check if temporary public directory exists *****/
if (Cached)
{
/* If not exists (it could be deleted if its lifetime has expired)
==> remove from cache */
TmpPubDirExists = Fil_CheckIfPathExists (TmpPubDir);
if (!TmpPubDirExists)
Brw_DB_DeletePublicDirFromCache (FullPathMediaPriv);
return TmpPubDirExists;
}
}
return false;
}
/*****************************************************************************/
/******** Get public directory used to link private path from cache **********/
/*****************************************************************************/
void Brw_DB_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1])
{
DB_QuerySELECTString (TmpPubDir,PATH_MAX,"can not get check if file is cached",
"SELECT TmpPubDir"
" FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,
FullPathMediaPriv);
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
static void Brw_DB_DeletePublicDirFromCache (const char *FullPathMediaPriv)
{
/***** Delete possible entry *****/
if (Gbl.Session.IsOpen)
DB_QueryDELETE ("can not remove cached file",
"DELETE FROM brw_caches"
" WHERE SessionId='%s'"
" AND PrivPath='%s'",
Gbl.Session.Id,FullPathMediaPriv);
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Brw_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1])
{
if (Gbl.Session.IsOpen)
{
/* Delete possible old entry */
Brw_DB_DeletePublicDirFromCache (FullPathMediaPriv);
/* Insert new entry */
Brw_DB_AddPublicDirToCache (FullPathMediaPriv,TmpPubDir);
}
}
/*****************************************************************************/
/********* Add public directory used to link private path to cache ***********/
/*****************************************************************************/
void Brw_DB_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1])
{
/* Insert new entry */
DB_QueryINSERT ("can not cache file",
"INSERT INTO brw_caches"
" (SessionId,PrivPath,TmpPubDir)"
" VALUES"
" ('%s','%s','%s')",
Gbl.Session.Id,
FullPathMediaPriv,
TmpPubDir);
}
/*****************************************************************************/
/****** Remove public directories used to link private paths from cache ******/
/*****************************************************************************/
void Brw_DB_RemovePublicDirsCache (void)
{
/***** Insert into cache *****/
if (Gbl.Session.IsOpen)
DB_QueryDELETE ("can not cache file",
"DELETE FROM brw_caches"
" WHERE SessionId='%s'",
Gbl.Session.Id);
}
/*****************************************************************************/
/****** Remove public directories used to link private paths from cache ******/
/****** (from expired sessions) ******/
/*****************************************************************************/
void Brw_DB_RemovePublicDirsFromExpiredSessions (void)
{
/***** Remove public directories in expired sessions *****/
DB_QueryDELETE ("can not remove public directories in expired sessions",
"DELETE FROM brw_caches"
" WHERE SessionId NOT IN"
" (SELECT SessionId"
" FROM ses_sessions)");
}

View File

@ -49,15 +49,4 @@ void Ses_RemoveParamFromThisSession (void);
void Ses_RemoveParamsFromExpiredSessions (void);
void Ses_GetParamFromDB (const char *ParamName,char *ParamValue,size_t StrSize);
bool Brw_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1]);
void Brw_DB_GetPublicDirFromCache (const char *FullPathMediaPriv,
char TmpPubDir[PATH_MAX + 1]);
void Brw_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1]);
void Brw_DB_AddPublicDirToCache (const char *FullPathMediaPriv,
const char TmpPubDir[PATH_MAX + 1]);
void Brw_DB_RemovePublicDirsCache (void);
void Brw_DB_RemovePublicDirsFromExpiredSessions (void);
#endif