コード例 #1
0
        /// <summary>
        /// AddFile function, getting a path to a file, adding it to the output directory.
        /// </summary>
        /// <param name="path">The file path</param>
        /// <param name="result">An out variable which will be true or false in accordance for fail or success </param>
        /// <returns>The result if file added successfully or not - this is the message we write into the log.</returns>
        public string AddFile(string path, out bool result)
        {
            if (!File.Exists(path))
            {
                result = false;
                return("The image you gave doesn't Exist!.");
            }
            //getting the path and name of the picture.
            string picPath = path;
            string picName = System.IO.Path.GetFileName(picPath);
            // Getting  the Creation time fo the picture, year and month.
            // If i used getcreationtime it wasn't the real creation time - we need the last time someone wrote to it
            //because when someone create picture first it is the first time he wrote to it.
            DateTime time     = File.GetLastWriteTime(picPath);
            int      picYear  = time.Year;
            int      picMonth = time.Month;
            //getting the path to year directory in the outputFolder directory
            string yearPath = System.IO.Path.Combine(m_OutputFolder, picYear.ToString());
            //getting te path to month directory in the year directory.
            string monthPath = System.IO.Path.Combine(yearPath, picMonth.ToString());
            //Building the thumbnail's path
            string thumbPath = Path.Combine(m_OutputFolder, "thumbnail");
            //The thumbnail's year path
            string thumbYearPath = Path.Combine(thumbPath, picYear.ToString());
            //The thumbnail's month path
            string thumbMonthPath = Path.Combine(thumbYearPath, picMonth.ToString());


            //If we added the extention
            string newPicName = "";

            //Building the directories
            try
            {
                //checking of the outputFolder exists or not- in case it doesn't exist, we create it.
                ImageFolderFunctions.CreateDirectory(m_OutputFolder);
                //Making the folder to hidden folder
                ImageFolderFunctions.MakeFolderHidden(m_OutputFolder);
                //if the year directory doesn't exist, we create it.
                ImageFolderFunctions.CreateDirectory(yearPath);
                //if the month directory doesn't exist, we create it.
                ImageFolderFunctions.CreateDirectory(monthPath);
                //creating the thumbnail directory
                ImageFolderFunctions.CreateDirectory(thumbPath);
                //creating the year directory in the thumbnail, if not existed, we create it.
                ImageFolderFunctions.CreateDirectory(thumbYearPath);
                //creating the month directory in thumbnail, if not existed, we create it.
                ImageFolderFunctions.CreateDirectory(thumbMonthPath);
            }
            catch (Exception e)
            {
                result = false;
                return(e.ToString());
            }
            //Copy the picture after creating all the directories
            try
            {
                newPicName = picName;
                //copying the image into the output folder.
                string newPath = monthPath + @"\" + picName;
                //First checking if it is already in the output folder
                if (!File.Exists(newPath))
                {
                    System.IO.File.Copy(picPath, newPath);
                }
                //The file is exist so we will copy in other name - for example for pic.jpg we will save pic_copy.jpg
                else
                {
                    while (File.Exists(newPath))
                    {
                        //Adding _copy until we have name of picture we do not have.
                        newPath = ImageFolderFunctions.addToImagePath(newPath, "_1");
                    }
                    newPicName = Path.GetFileName(newPath);
                    System.IO.File.Copy(picPath, newPath);
                    //Adding copy to the pic name.
                }
            }
            catch (Exception e)
            {
                result = false;
                return("Problem copying the image into the output folder\n\nImage:" + picPath + "\n\n" + e.ToString());
            }
            Image image = Image.FromFile(monthPath + @"\" + picName);
            //Creating the thumbnail.
            Image thumb = image.GetThumbnailImage(this.m_thumbnailSize, this.m_thumbnailSize, () => false, IntPtr.Zero);

            try
            {
                string newThumbPath = thumbMonthPath + @"\" + newPicName;
                //First check if the thumbnail is already in the output folder
                if (!File.Exists(thumbMonthPath + @"\" + newPicName))
                {
                    //Saving the thumbnail with original extention
                    thumb.Save(newThumbPath);
                }
            }
            catch
            {
                result = false;
                return("Problem saving the thumbnail picture\n\nImage:" + picPath);
            }
            //Delete the original picture
            try
            {
                File.Delete(picPath);
            }
            catch
            {
                result = false;
                return("Problem deleting the copied picture");
            }

            //setting result to true since the image moved successfully.
            result = true;
            return("Image: " + newPicName + " was added successfully\n\nIt is on the path: " + monthPath);
        }
コード例 #2
0
        public void HandleClient(TcpClient client, object locker)
        {
            //creating a task that will handle the client's commands.
            Task t = new Task(() =>
            {
                NetworkStream stream = client.GetStream();
                BinaryReader reader  = new BinaryReader(stream);
                BinaryWriter writer  = new BinaryWriter(stream);
                while (true)
                {
                    //Getting the command.
                    //readMutex.WaitOne();

                    /**
                     * first byte has the size of the picture.
                     * Than we get the size of the size of the image in the byte array.
                     * Than we get the size of the image.
                     * after the firsty comes the picture as bytes.
                     * after building the picture from its bytes, we move it to one of the handled folders (haha, aa, ...)
                     * and thats it.
                     * */
                    byte[] check = reader.ReadBytes(1);

                    if (check[0] == 1)
                    {
                        Console.WriteLine("\nStarting to transfer new picture!!");
                        string picName = ReadString(reader);
                        Console.WriteLine("Pic name is " + picName);
                        int nBytes;


                        int sizeSizeImage = reader.ReadBytes(1)[0];
                        byte[] sizeImage  = reader.ReadBytes(sizeSizeImage);
                        var str           = System.Text.Encoding.Default.GetString(sizeImage);
                        nBytes            = int.Parse(str);

                        Console.WriteLine("Picture size is " + nBytes + " bytes");



                        //Reading the image
                        Image photo;
                        MemoryStream ms = new MemoryStream(reader.ReadBytes(nBytes));
                        photo           = Image.FromStream(ms);

                        string date = ReadString(reader);

                        Configure config = Configure.GetInstance();
                        string path      = config.Handlers[0];
                        string picPath   = Path.Combine(path, picName);
                        string tempPath  = Path.Combine(path, "temp");
                        ImageFolderFunctions.CreateDirectory(tempPath);
                        ImageFolderFunctions.MakeFolderHidden(tempPath);

                        tempPath = Path.Combine(tempPath, picName);

                        photo.Save(tempPath);
                        Console.WriteLine("Date is " + date);
                        DateTime taken = DateTime.ParseExact(date, "MM/dd/yyyy HH:mm:ss",
                                                             System.Globalization.CultureInfo.InvariantCulture);
                        try
                        {
                            File.SetLastWriteTime(tempPath, taken);
                        } catch (Exception)
                        {
                        }

                        ImageFolderFunctions.ChangeDirectory(tempPath, picPath);
                        Thread.Sleep(2000);

                        continue;
                    }
                    //Finish connection
                    //if (check[0] == 0)
                    //{
                    //    Console.WriteLine("\nFinished");
                    //    break;
                    //}
                    //Locking this critical place
                    //lock (locker)
                    //{
                    //    writer.Write(message.ToJSON());
                    //}
                }
                //client.Close();
            });

            t.Start();
        }