From 9e9f93baea39a96345e2fe66554ac020416a8961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Fri, 1 Apr 2016 12:47:32 +0200 Subject: [PATCH] Version 15.172 --- swad_centre.c | 2 +- swad_changelog.h | 3 +- swad_file.c | 11 ++++++-- swad_test.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/swad_centre.c b/swad_centre.c index 53682aabe..9124d1650 100644 --- a/swad_centre.c +++ b/swad_centre.c @@ -2066,7 +2066,7 @@ void Ctr_ReceivePhoto (void) char Command[1024+PATH_MAX*2]; int ReturnCode; - /***** Copy in disk the file received from stdin (really from Gbl.F.Tmp) *****/ + /***** Copy in disk the file received *****/ Param = Fil_StartReceptionOfFile (FileNameImgSrc,MIMEType); /* Check if the file type is image/ or application/octet-stream */ diff --git a/swad_changelog.h b/swad_changelog.h index 82e1f2178..6fabe91f7 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -138,13 +138,14 @@ /****************************** Public constants *****************************/ /*****************************************************************************/ -#define Log_PLATFORM_VERSION "SWAD 15.171.2 (2016-04-01)" +#define Log_PLATFORM_VERSION "SWAD 15.172 (2016-04-01)" #define CSS_FILE "swad15.165.5.css" #define JS_FILE "swad15.131.3.js" // Number of lines (includes comments but not blank lines) has been got with the following command: // nl swad*.c swad*.h css/swad*.css py/swad*.py js/swad*.js soap/swad*.h sql/swad*.sql | tail -1 /* + Version 15.172: Apr 01, 2016 Reception of test image. Not finished. (197339 lines) Version 15.171.2: Apr 01, 2016 Change in message related to user photo. (197272 lines) Version 15.171.1: Apr 01, 2016 Code refactoring in functions to get a parameter and to receive file. (197271 lines) Version 15.171: Apr 01, 2016 Code refactoring in function to get a parameter. (197259 lines) diff --git a/swad_file.c b/swad_file.c index 289534831..5daff8826 100644 --- a/swad_file.c +++ b/swad_file.c @@ -224,6 +224,9 @@ struct Param *Fil_StartReceptionOfFile (char *FileName,char *MIMEType) { struct Param *Param; + /***** Set default values *****/ + FileName[0] = 0; + /***** Get parameter *****/ Par_GetParameter (Par_PARAM_SINGLE,Fil_NAME_OF_PARAM_FILENAME_ORG,NULL, Fil_MAX_FILE_SIZE,&Param); @@ -231,8 +234,12 @@ struct Param *Fil_StartReceptionOfFile (char *FileName,char *MIMEType) /***** Get filename *****/ /* Check if filename exists */ if (Param->FileName.Start == 0 || - Param->FileName.Length == 0 || - Param->FileName.Length > PATH_MAX) + Param->FileName.Length == 0) + { + FileName[0] = MIMEType[0] = '\0'; + return Param; + } + if (Param->FileName.Length > PATH_MAX) Lay_ShowErrorAndExit ("Error while getting filename."); /* Copy filename */ diff --git a/swad_test.c b/swad_test.c index ceeb1a68f..fa53243a0 100644 --- a/swad_test.c +++ b/swad_test.c @@ -36,6 +36,7 @@ #include // For string functions #include // For mkdir #include // For mkdir +#include // For unlink #include "swad_action.h" #include "swad_database.h" @@ -206,6 +207,7 @@ static int Tst_CountNumAnswerTypesInList (void); static void Tst_PutFormEditOneQst (char *Stem,char *Feedback); static Tst_AnswerType_t Tst_ConvertFromUnsignedStrToAnsTyp (const char *UnsignedStr); static void Tst_GetQstFromForm (char *Stem,char *Feedback); +static bool Tst_GetImageFromForm (void); static long Tst_GetTagCodFromTagTxt (const char *TagTxt); static long Tst_CreateNewTag (long CrsCod,const char *TagTxt); static void Tst_EnableOrDisableTag (long TagCod,bool TagHidden); @@ -4641,6 +4643,7 @@ void Tst_InitQst (void) Gbl.Test.Stem.Length = 0; Gbl.Test.Feedback.Text = NULL; Gbl.Test.Feedback.Length = 0; + Gbl.Test.Image[0] = '\0'; Gbl.Test.AnswerType = Tst_ANS_UNIQUE_CHOICE; Gbl.Test.Answer.NumOptions = 0; Gbl.Test.Answer.TF = ' '; @@ -4778,6 +4781,10 @@ static void Tst_GetQstFromForm (char *Stem,char *Feedback) /***** Get question feedback *****/ Par_GetParToHTML ("Feedback",Feedback,Cns_MAX_BYTES_TEXT); + /***** Get image *****/ + if (Tst_GetImageFromForm ()) + Lay_ShowAlert (Lay_INFO,"Image present."); + /***** Get answers *****/ Gbl.Test.Shuffle = false; switch (Gbl.Test.AnswerType) @@ -4876,6 +4883,71 @@ static void Tst_GetQstFromForm (char *Stem,char *Feedback) Gbl.Test.Feedback.Length = strlen (Gbl.Test.Feedback.Text); } +/*****************************************************************************/ +/****************** Get image of a test question from form *******************/ +/*****************************************************************************/ +// Return true if image is created + +static bool Tst_GetImageFromForm (void) + { + struct Param *Param; + char FileNameImgSrc[PATH_MAX+1]; + char *PtrExtension; + size_t LengthExtension; + char MIMEType[Brw_MAX_BYTES_MIME_TYPE+1]; + char PathImgPriv[PATH_MAX+1]; + char FileNameImgTmp[PATH_MAX+1]; // Full name (including path and .jpg) of the destination temporary file + bool WrongType = false; + + /***** Get filename and MIME type *****/ + Param = Fil_StartReceptionOfFile (FileNameImgSrc,MIMEType); + if (!FileNameImgSrc[0]) // No file present + return false; + + /* Check if the file type is image/ or application/octet-stream */ + if (strncmp (MIMEType,"image/",strlen ("image/"))) + if (strcmp (MIMEType,"application/octet-stream")) + if (strcmp (MIMEType,"application/octetstream")) + if (strcmp (MIMEType,"application/octet")) + WrongType = true; + if (WrongType) + return false; + + /***** Create private directories if not exist *****/ + /* Create private directory for images if it does not exist */ + sprintf (PathImgPriv,"%s/%s", + Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_IMG); + Fil_CreateDirIfNotExists (PathImgPriv); + + /* Create temporary private directory for images if it does not exist */ + sprintf (PathImgPriv,"%s/%s/%s", + Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_IMG,Cfg_FOLDER_IMG_TMP); + Fil_CreateDirIfNotExists (PathImgPriv); + + /* Get filename extension */ + if ((PtrExtension = strrchr (FileNameImgSrc,(int) '.')) == NULL) + return false; + LengthExtension = strlen (PtrExtension); + if (LengthExtension < Fil_MIN_LENGTH_FILE_EXTENSION || + LengthExtension > Fil_MAX_LENGTH_FILE_EXTENSION) + return false; + + /* End the reception of image in a temporary file */ + strcpy (Gbl.Test.Image,Gbl.UniqueNameEncrypted); + sprintf (FileNameImgTmp,"%s/%s/%s/%s.%s", + Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_IMG,Cfg_FOLDER_IMG_TMP, + Gbl.Test.Image,PtrExtension); + if (!Fil_EndReceptionOfFile (FileNameImgTmp,Param)) + return false; + + /***** TODO: Copy and process the file *****/ + + /***** Remove temporary file *****/ + unlink (FileNameImgTmp); + + return true; + } + /*****************************************************************************/ /*********************** Check if a question is correct **********************/ /*****************************************************************************/