Beispiel #1
0
        // recursively scan files in directories and check if it's uploaded or not.
        // returns true if the specified folder exists
        private static bool scanFilesAndUpload(String postFileURL, String directoryPath, String projectName, String subjectId, String dataType, String subDirectoryPath, String[] excludeDirectoryPaths, String notes)
        {
            try
            {   //Check if local directory exist
                if (!Directory.Exists(directoryPath))
                {
                    return false;
                }
                else
                {
                    //--- check the directory files -----
                    #region Scan files in Directory

                    if (excludeDirectoryPaths != null)
                    {
                        for (int i = 0; i < excludeDirectoryPaths.Length; i++)
                        {
                            if (directoryPath == excludeDirectoryPaths[i])
                            {
                                return false;
                            }
                        }
                    }

                    try
                    {
                        string[] fileList = Directory.GetFiles(directoryPath);

                        Dictionary<String, String> uploadedList = new Dictionary<String, String>();

                        if (fileList != null)
                        {

                            //Get File to upload
                            String pathToHistoryFile = directoryPath + "\\" + fileUploadHistoryFileName;
                            if (File.Exists(pathToHistoryFile))
                            {
                                fileUploadHistoryReader = File.OpenText(pathToHistoryFile);
                                string input = null;
                                while ((input = fileUploadHistoryReader.ReadLine()) != null)
                                {
                                    // filename,last write time
                                    String[] splittedLine = input.Split(',');
                                    uploadedList.Add(splittedLine[0], splittedLine[1]);
                                }
                                fileUploadHistoryReader.Close();

                                // check files to see if it's uploaded or not
                                for (int i = 0; i < fileList.Length; i++)
                                {
                                    // skip file if it's the log file
                                    if (Path.GetFileName(fileList[i]) == fileUploadHistoryFileName)
                                        continue;

                                    //Here add the File to upload to the filetoupload list
                                    if (uploadedList.ContainsKey(fileList[i]))
                                    {
                                        FileInfo finfo = new FileInfo(fileList[i]);

                                        // now check last write time
                                        if (uploadedList[fileList[i]] != finfo.LastWriteTime.ToString())
                                        {
                                            // time not equal, upload required.
                                            //FileToSend fileToSend = new FileToSend(postFileURL, directoryPath + "/" + fileList[i], projectName, subjectId, dataType, subDirectoryPath, notes);
                                            FileToSend fileToSend = new FileToSend(postFileURL, directoryPath + "/" + fileList[i],  projectName, subjectId, dataType, subDirectoryPath, notes);
                                            fileUploadQueue.Add(fileToSend);
                                        }
                                        else
                                        {
                                            if (enableLogging)
                                            {
                                                logWriter.WriteLine(DateTime.Now + "," + fileList[i] + ", already uploaded.");
                                                logWriter.Flush();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // new file, upload required.
                                        //FileToSend fileToSend = new FileToSend(postFileURL, fileList[i], projectName, subjectId, dataType, subDirectoryPath, notes);
                                        FileToSend fileToSend = new FileToSend(postFileURL, fileList[i], projectName, subjectId, dataType, subDirectoryPath, notes);
                                        fileUploadQueue.Add(fileToSend);
                                    }
                                }
                            }
                            else // the flog.txt do not exist, upload everything.
                            {

                                // check files to see if it's uploaded or not
                                for (int i = 0; i < fileList.Length; i++)
                                {
                                    //FileToSend fileToSend = new FileToSend(postFileURL, fileList[i], projectName, subjectId, dataType, subDirectoryPath, notes);
                                    FileToSend fileToSend = new FileToSend(postFileURL, fileList[i], projectName, subjectId, dataType, subDirectoryPath, notes);
                                    fileUploadQueue.Add(fileToSend);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (enableLogging)
                        {
                            logWriter.WriteLine("Error in Check directory files:" + e.Message);
                            logWriter.Flush();
                        }
                    }

                    #endregion

                    //----- Check directories  ---------------
                    #region Scan directories

                    try
                    {
                        string[] dirList = Directory.GetDirectories(directoryPath);

                        if (dirList != null)
                        {

                            for (int i = 0; i < dirList.Length; i++)
                            {
                                String newSubDirectoryPath = dirList[i].Substring(selectedDirectoryPath.Length);

                                //Call the function recursively to upload all files in the subfolder.
                                scanFilesAndUpload(postFileURL, dirList[i], projectName, subjectId, dataType, FileUploader.selectedSubDirectoryPath + newSubDirectoryPath, excludeDirectoryPaths, notes);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (enableLogging)
                        {
                            logWriter.WriteLine("Error in Check directories:" + e.Message);
                            logWriter.Flush();
                        }
                    }

                    #endregion

                }
            }
            catch (Exception e)
            {
                if (enableLogging)
                {
                    logWriter.WriteLine("Error:" + e.Message);
                    logWriter.Flush();
                }
                return false;
            }

            return true;
        }
Beispiel #2
0
 // upload files
 public static void uploadFile(String postFileURL, String pathToFile, String nameFile, String applicationName, String subjectId, String dataType, String subDirectoryPath, String notes)
 {
     //FileToSend fileToSend = new FileToSend(postFileURL, pathToFile, projectName, subjectId, dataType, subDirectoryPath, notes);
     FileToSend fileToSend = new FileToSend(postFileURL, pathToFile, applicationName, subjectId, dataType, subDirectoryPath, notes);
     fileUploadQueue.Add(fileToSend);
 }