|
The other day I have asked a question about a program which sits in a place where images come and once its started, function is that it updates when a new image is added, I have found a relevant code for this but I would like to know that would this do the desired task which I require. Appreciate any assistance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foundation;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Threading;
using WindowsFormsApplication1;
namespace HotFolderLoader
{
class Program
{
//private BaseFuncs theBaseFuncs = new BaseFuncs();
static void Main(string[] args)
{
string tt = System.Environment.MachineName;
Foundation.BaseFuncs theFoundation = new BaseFuncs();
if (tt == "NEO") //devel
{
//Foundation.getConstr.initialize("Data Source=neo\\neosqlserver;Initial Catalog=nmcvisualdb;Persist Security Info=True;User ID=sa;Password=xxxxxx");
Foundation.getConstr.initialize("Data Source=xx.xxx.xxx.xxx\\bcwebsql;Initial Catalog=nmcvisualdb;Persist Security Info=True;User ID=sa;Password=mr2BCweb");
//Foundation.getConstr.initialize("Data Source=xxx.xx.xxx.xxx\\nmcsql;Initial Catalog=nmcvisualdb;Persist Security Info=True;User ID=sa;Password=xxxxx");
}
else
{
Foundation.getConstr.initialize("Data Source=xx.xxx.xxx.xxx\\bcwebsql;Initial Catalog=nmcvisualdb;Persist Security Info=True;User ID=sa;Password=xxxxx");
}
//CCaseObject gg = new CCaseObject("test1-lil");
int idx = 0;
while (true)
{
System.Console.WriteLine("Starting");
doWork();
idx++;
System.Console.WriteLine("parsing: " + idx.ToString());
Thread.Sleep(80 * 1000);
}
}
public static void doWork()
{
BaseFuncs theBaseFuncs = new Foundation.BaseFuncs();
//D:\Google Drev\CloudHotFolder
//E:\google-drive\Google Drive\CloudHotFolder
DirectoryInfo dirInfo = new DirectoryInfo(@"D:\Google Drev\CloudHotFolder");
//DirectoryInfo dirInfo = new DirectoryInfo(@"E:\google-drive\Google Drive\CloudHotFolder");
FileInfo[] files = dirInfo.GetFiles("*.jpg");
string caseno = "";
string imageType = "";
string previousCaseno = "";
int i = 0;
System.Console.WriteLine("files: " + files.Count().ToString());
while (i < files.Count())
{
FileInfo fileItem = files[i];
bool nameOK = true;
bool isError = fileItem.Name.Contains("Error");
string[] fileData = fileItem.Name.Split('#');
if (fileData.Length < 2)
{
nameOK = false;
}
else
{
caseno = fileData[1];
imageType = fileData[2];
}
System.Console.WriteLine("parsing file: " + fileItem.FullName);
/*
* photo_org = 1,
floor_plan_org = 2,
Panorama_360 = 3,
photo_stage_2 = 4,
floor_plan_stage_2 = 5,
photo_thumbnail = 6,
Panorama_360_stage_2 = 7,
photo_final = 8,
floor_plan_final = 9,
Panorama_360_final = 10,
deleted = 11,
junk = 12,
visual_tour_org = 12,
visual_tour = 13
* */
string imgtypeid = "";
bool typeOK = true;
bool caseOK = true;
switch (imageType)
{
case "1":
case "4":
imgtypeid = "4"; //photo stage 2
break;
case "2":
case "5":
imgtypeid = "5"; //floor plan stage 2
break;
case "3":
case "7":
imgtypeid = "7";
break;
default:
typeOK = false;
break;
}
string sql = "select count(id) from cases where caseno ='" + caseno + "'";
string casecount = theBaseFuncs.doSQLScalar(sql);
if (casecount != "1")
{
caseOK = false;
}
if (typeOK && !isError && caseOK && nameOK)
{
try
{ //(expiryDate - DateTime.Now).Days < 30
if ((DateTime.Now - fileItem.LastAccessTime).Minutes > 1 && fileItem.Length > 10)
{
//check if filename exists
string checkSql = "select count (id) from images where case_id=" + theBaseFuncs.getCaseIDByCaseNo(caseno) + " and image_name='" + fileItem.Name + "'";
int count = Convert.ToInt16(theBaseFuncs.doSQLScalar(checkSql));
if (count > 0)
{
//delete image in db - and then normal upload new version
System.Console.Write("Deleting " + fileItem.Name + "in DB");
string delImages = "delete from images where case_id=" + theBaseFuncs.getCaseIDByCaseNo(caseno) + " and image_name='" + fileItem.Name + "'";
theBaseFuncs.doSQLnonQuery(delImages);
System.Console.Write("...Done");
}
int picID = theBaseFuncs.saveIMG2DB(fileItem, fileItem.Name, "image/jpeg", caseno, imgtypeid, 5, false);
SqlConnection connection = new SqlConnection("Data Source=xx.xxx.xxx.xxx\\bcwebsql;Initial Catalog=nmcvisualdb;Persist Security Info=True;User ID=sa;Password=xxxxx");
System.Drawing.Image img = System.Drawing.Image.FromFile(fileItem.FullName);
//System.Drawing.Image thumbImg = createHQThumb(fullImg, xparm);
string strSql = "UPDATE Images set Image_Data_Thumb=@imagedata ";
strSql += "where id=@picid";
SqlCommand updatecmd = new SqlCommand(strSql, connection);
updatecmd.CommandType = CommandType.Text;
SqlParameter pImg = new SqlParameter("@imagedata", SqlDbType.Image);
pImg.Value = BaseFuncs.imageToByteArray(BaseFuncs.createHQThumb(img, 600));
img.Dispose();
updatecmd.Parameters.Add(pImg);
SqlParameter pPicid = new SqlParameter("@picid", SqlDbType.Int);
pPicid.Value = picID;
updatecmd.Parameters.Add(pPicid);
connection.Open();
updatecmd.ExecuteNonQuery();
fileItem.Delete();
connection.Close();
//System.Console.WriteLine("counter: i=" + i.ToString() + " max=" + files.Count().ToString());
}
if (caseno != previousCaseno || i == files.Count() - 1) //do target check
{
if (i == files.Count())
{
System.Console.WriteLine("swapping caseno - lastfile in list!");
previousCaseno = caseno;
}
System.Console.Write("Making target check case: " + previousCaseno);
theBaseFuncs.logStatus(theBaseFuncs.getCaseIDByCaseNo(caseno), 3, 36, "Hotfolder target check");
if (theBaseFuncs.targetImgCheck(previousCaseno) == 1 && theBaseFuncs.targetPlanCheck(previousCaseno) == 1)
{
System.Console.Write("...target match");
Foundation.CCaseObject theCase = new Foundation.CCaseObject(previousCaseno);
theBaseFuncs.logStatus(theCase.CaseID, 3, 36, "Hotfolder target OK");
theCase.sendToNaming();
System.Console.Write("...case sent!!!!!!!!\n");
}
else
{
theBaseFuncs.logStatus(theBaseFuncs.getCaseIDByCaseNo(caseno), 3, 36, "Hotfolder target not OK");
System.Console.Write(".no match!\n");
}
}
previousCaseno = caseno;
}
catch (Exception ex)
{
string gg = ex.Message;
throw;
}
}
else
{
if (!isError)
{
fileItem.MoveTo(fileItem.FullName + ".type_error");
}
if (!caseOK)
{
fileItem.MoveTo(fileItem.FullName + ".caseno_error");
}
if (!nameOK)
{
fileItem.MoveTo(fileItem.FullName + ".navngivning_error");
}
}
i++;
}
}
}
}
modified 5-Oct-12 4:08am.
|
|
|
|
|
Firstly your code is not formatted so it is very difficult to read, please surround it with <pre> tags. Secondly, people here are unlikely to spend time analysing this code for you. Try it yourself to see whether it suits your needs and come back here when you have a more specific question.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
This is totally the wrong way to ask a question. See here[^].
First try yourself, if you get stuck or don't understand a specific part of it, come back here.
Nobody will do the work for you, but generally will point you in the right direction if they can.
|
|
|
|