diff options
Diffstat (limited to 'src/server.cc')
-rw-r--r-- | src/server.cc | 175 |
1 files changed, 167 insertions, 8 deletions
diff --git a/src/server.cc b/src/server.cc index 3b69c64..27b6848 100644 --- a/src/server.cc +++ b/src/server.cc @@ -26,6 +26,9 @@ */ /* * $Log$ + * Revision 1.9 2005/05/02 18:46:15 deva + * Files are now saved in a custom folder (defined in miav.conf) + * * Revision 1.8 2005/05/01 09:56:26 deva * Added Id and Log tags to all files * @@ -36,6 +39,15 @@ #include <stdio.h> #include <stdlib.h> +// For mkdir +#include <sys/stat.h> +#include <sys/types.h> + +// For unlink +#include <unistd.h> + +#include "miav_config.h" + #include "mov_encoder.h" #include "img_encoder.h" @@ -45,11 +57,88 @@ void saveFrameAsImage(char* cpr, Frame *f) { - char fname[256]; ImgEncoder imgenc; + struct tm *ltime; + time_t t = time(NULL); + FILE *fp; + int cnt = 0; + char fname[256]; + char birthmonth[3]; + char date[9]; + + string *root = config->readString("server_root"); + + // Test for server root writeability + sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand()); + fp = fopen(fname, "w"); + if(!fp) { + int r = rand(); + fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str()); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best. + return; + } + fclose(fp); + unlink(fname); - sprintf(fname, "image-%s-%d.jpeg", cpr, rand()); + // Check for cpr length correctness + if(strlen(cpr) != 11) { + int r = rand(); + fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr)); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best. + return; + } + + // Copy the bytes representing the birth month from the cpr + // [dd][mm][yy]-[nn][nn] + strncpy(birthmonth, &cpr[2], 2); + birthmonth[2] = 0; + + // Create folder named birthmonth in server root + sprintf(fname, "%s/%s", root->c_str(), birthmonth); + if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) { + int r = rand(); + fprintf(stderr, "Not possible to create subfolder %s\n", fname); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best. + return; + } + + // Create folder named cpr in serverroot/birthmonth + sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr); + if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) { + int r = rand(); + fprintf(stderr, "Not possible to create subfolder %s\n", fname); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best. + return; + } + + // Create date (today) in [yyyy][mm][dd] + ltime = localtime(&t); + sprintf(date, "%.4d%.2d%.2d", + ltime->tm_year + 1900, + ltime->tm_mon, + ltime->tm_mday); + // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg + sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt); + + // test filename-[cnt] for existamce cnt++ until not existing. + fp = fopen(fname, "r"); + while(fp) { + fclose(fp); + cnt++; + sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt); + fp = fopen(fname, "r"); + } + + fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr); imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best. } /* @@ -71,16 +160,86 @@ MovEncoder *newMovEncoder(char* cpr) { MovEncoder *enc; struct tm *ltime; - char fname[256]; time_t t = time(NULL); + FILE *fp; + int cnt = 0; + char fname[256]; + char birthmonth[3]; + char date[9]; + + string *root = config->readString("server_root"); + + // Test for server root writeability + sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand()); + fp = fopen(fname, "w"); + if(!fp) { + int r = rand(); + fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str()); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + enc = new MovEncoder(fname); + return enc; + } + fclose(fp); + unlink(fname); + + // Check for cpr length correctness + if(strlen(cpr) != 11) { + int r = rand(); + fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr)); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + enc = new MovEncoder(fname); + return enc; + } + + // Copy the bytes representing the birth month from the cpr + // [dd][mm][yy]-[nn][nn] + strncpy(birthmonth, &cpr[2], 2); + birthmonth[2] = 0; + + // Create folder named birthmonth in server root + sprintf(fname, "%s/%s", root->c_str(), birthmonth); + if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) { + int r = rand(); + fprintf(stderr, "Not possible to create subfolder %s\n", fname); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + enc = new MovEncoder(fname); + return enc; + } + + // Create folder named cpr in serverroot/birthmonth + sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr); + if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) { + int r = rand(); + fprintf(stderr, "Not possible to create subfolder %s\n", fname); + fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r); + sprintf(fname, "/tmp/miav-%d.mpg", r); + enc = new MovEncoder(fname); + return enc; + } + + // Create date (today) in [yyyy][mm][dd] ltime = localtime(&t); - sprintf(fname, "%.2d%.2d%.2d%.2d%.2d%.2d-%s.mpg", + sprintf(date, "%.4d%.2d%.2d", ltime->tm_year + 1900, ltime->tm_mon, - ltime->tm_mday, - ltime->tm_hour, - ltime->tm_min, - ltime->tm_sec, cpr); + ltime->tm_mday); + + // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg + sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt); + + // test filename-[cnt] for existamce cnt++ until not existing. + fp = fopen(fname, "r"); + while(fp) { + fclose(fp); + cnt++; + sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt); + fp = fopen(fname, "r"); + } + + fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr); enc = new MovEncoder(fname); return enc; } |