swad-core/swad_media.h

167 lines
6.8 KiB
C
Raw Permalink Normal View History

2019-03-02 21:49:28 +01:00
// swad_media.h: processing of image/video uploaded in a form
#ifndef _SWAD_MED
#define _SWAD_MED
2019-03-02 21:49:28 +01:00
/*
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-2024 Antonio Ca<EFBFBD>as Vargas
2019-03-02 21:49:28 +01:00
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 ***********************************/
/*****************************************************************************/
2020-05-07 12:56:47 +02:00
#include <mysql/mysql.h> // To access MySQL databases
2020-06-24 20:47:54 +02:00
#include "swad_cryptography.h"
#include "swad_string.h"
2020-06-24 20:47:54 +02:00
2019-03-02 21:49:28 +01:00
/*****************************************************************************/
/***************************** Public constants ******************************/
/*****************************************************************************/
#define Med_BYTES_NAME Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64
#define Med_MAX_CHARS_TITLE (128 - 1) // 127
#define Med_MAX_BYTES_TITLE ((Med_MAX_CHARS_TITLE + 1) * Str_MAX_BYTES_PER_CHAR - 1) // 2047
#define Med_MAX_CHARS_ATTRIBUTION (256 - 1) // 255
#define Med_MAX_BYTES_ATTRIBUTION ((Med_MAX_CHARS_ATTRIBUTION + 1) * Str_MAX_BYTES_PER_CHAR - 1) // 4095
/*****************************************************************************/
/******************************* Public types ********************************/
/*****************************************************************************/
/***** Action to perform when editing a form with an image/video *****/
2019-03-17 14:47:58 +01:00
#define Med_NUM_ACTIONS 3
2019-03-02 21:49:28 +01:00
typedef enum
{
2019-03-15 15:25:31 +01:00
Med_ACTION_NO_MEDIA, // Do not use media (remove current media if exists)
2019-03-17 14:47:58 +01:00
Med_ACTION_KEEP_MEDIA, // Keep current media unchanged
Med_ACTION_NEW_MEDIA, // Upload new media (if current media exists, remove and change by the new one)
2019-03-02 21:49:28 +01:00
} Med_Action_t;
#define Med_ACTION_DEFAULT Med_ACTION_NO_MEDIA
/***** Status of an image/video file *****/
/*
No file Original file Temporary Definitive Name of the image/video
uploaded uploaded by user processed file processed file stored in database
--------- --------------------------- ------------------ ------------------ ---------------------
2019-03-18 15:42:22 +01:00
Med_STATUS_NONE Med_PROCESSED Med_MOVED Med_NAME_STORED_IN_DB
2019-03-02 21:49:28 +01:00
--------- --------------------------- ------------------ ------------------ ---------------------
2019-03-17 14:47:58 +01:00
upload-file process file move file insert in database
2019-03-02 21:49:28 +01:00
--------- --------------------------- ------------------ ------------------ ---------------------
2019-03-17 14:47:58 +01:00
file.ext / / / xx-unique-name
| | |
var var var
| | |
www www www
| | |
swad swad swad
| | |
med med med
| | |
tmp tmp xx (2 first chars)
| | |
2019-03-02 21:49:28 +01:00
xx-unique-name_original.ext xx-unique-name.jpg xx-unique-name.jpg
xx-unique-name: a unique name encrypted starting by two random chars xx
*/
typedef enum
{
2019-03-15 15:25:31 +01:00
Med_STATUS_NONE,
Med_PROCESSED,
2019-03-18 15:42:22 +01:00
Med_MOVED,
2019-03-15 15:25:31 +01:00
Med_STORED_IN_DB,
} Med_Status_t;
2019-03-02 21:49:28 +01:00
2019-03-21 20:04:01 +01:00
#define Med_NUM_TYPES 8
2019-03-02 21:49:28 +01:00
typedef enum
{
2019-03-15 15:25:31 +01:00
Med_TYPE_NONE,
2019-03-02 21:49:28 +01:00
Med_JPG,
Med_GIF,
2019-03-13 23:04:00 +01:00
Med_MP4,
Med_WEBM,
Med_OGG,
2019-03-15 15:25:31 +01:00
Med_YOUTUBE,
2019-03-21 20:04:01 +01:00
Med_EMBED,
2019-03-02 21:49:28 +01:00
} Med_Type_t;
/***** Struct used to get images/videos from forms *****/
2021-02-11 00:58:53 +01:00
struct Med_Media
2019-03-02 21:49:28 +01:00
{
2019-03-18 15:42:22 +01:00
long MedCod;
2019-03-02 21:49:28 +01:00
Med_Action_t Action;
2019-03-15 15:25:31 +01:00
Med_Status_t Status;
2019-03-02 21:49:28 +01:00
char Name[Med_BYTES_NAME + 1];
Med_Type_t Type;
2019-03-17 14:47:58 +01:00
bool URLIsAllocated;
bool TitleIsAllocated;
2019-03-02 21:49:28 +01:00
char *URL; // URL, i.e. link to original big photo or video
// (it must be initialized to NULL
// in order to not trying to free it when no memory allocated)
2019-03-17 14:47:58 +01:00
char *Title; // Title/attribution (it must be initialized to NULL
// in order to not trying to free it when no memory allocated)
2019-03-02 21:49:28 +01:00
unsigned Width;
unsigned Height;
unsigned Quality;
};
/***** Parameters used in a form to upload an image/video *****/
2019-12-19 01:42:24 +01:00
#define Med_MAX_BYTES_PARAM_UPLOAD_MEDIA (32 - 1)
struct Med_ParUpload
2019-03-02 21:49:28 +01:00
{
2019-03-15 15:25:31 +01:00
char Action [Med_MAX_BYTES_PARAM_UPLOAD_MEDIA + 1];
2019-03-17 01:38:10 +01:00
char FormType[Med_MAX_BYTES_PARAM_UPLOAD_MEDIA + 1];
2019-03-15 15:25:31 +01:00
char File [Med_MAX_BYTES_PARAM_UPLOAD_MEDIA + 1];
char Title [Med_MAX_BYTES_PARAM_UPLOAD_MEDIA + 1];
char URL [Med_MAX_BYTES_PARAM_UPLOAD_MEDIA + 1];
2019-03-02 21:49:28 +01:00
};
/*****************************************************************************/
/***************************** Public prototypes *****************************/
/*****************************************************************************/
2021-02-11 00:58:53 +01:00
void Med_MediaConstructor (struct Med_Media *Media);
void Med_MediaDestructor (struct Med_Media *Media);
void Med_ResetMedia (struct Med_Media *Media);
2019-03-02 21:49:28 +01:00
2021-02-11 00:58:53 +01:00
void Med_GetMediaDataByCod (struct Med_Media *Media);
2019-03-02 21:49:28 +01:00
void Med_PutMediaUploader (int NumMedia,const char *ClassInput);
void Med_GetMediaFromForm (long CrsCod,long QstCod,int NumMedia,struct Med_Media *Media,
void (*GetMediaFromDB) (long CrsCod,long QstCod,int NumMedia,struct Med_Media *Media),
2019-03-17 14:47:58 +01:00
const char *SectionForAlerts);
void Med_SetParsNames (struct Med_ParUpload *ParUpl,int NumMedia);
2019-03-02 21:49:28 +01:00
2021-02-11 00:58:53 +01:00
void Med_RemoveKeepOrStoreMedia (long CurrentMedCodInDB,struct Med_Media *Media);
void Med_MoveMediaToDefinitiveDir (struct Med_Media *Media);
void Med_StoreMediaInDB (struct Med_Media *Media);
2019-03-18 15:42:22 +01:00
2021-02-11 00:58:53 +01:00
void Med_ShowMedia (const struct Med_Media *Media,
2019-03-02 21:49:28 +01:00
const char *ClassContainer,const char *ClassMedia);
2021-02-11 00:58:53 +01:00
long Med_CloneMedia (const struct Med_Media *MediaSrc);
2020-05-13 00:28:32 +02:00
2019-03-18 15:42:22 +01:00
void Med_RemoveMediaFromAllRows (unsigned NumMedia,MYSQL_RES *mysql_res);
void Med_RemoveMedia (long MedCod);
2019-03-02 21:49:28 +01:00
#endif