Exemplo n.º 1
0
        public static void UploadFilesToGoogleDriveFolder(int userId, FileInfo[] files)
        {
            DriveService service = RegisterServiceByUserId(userId);

            // Get folder id of evatel sound records
            string folderId = GetGoogleDriveFolderId(Globals.CLOUD_EVATEL_FOLDER, service);

            // Folder has not been existing yet
            if (folderId == null)
            {
                // Create a Evatel folder, and return folder id
                folderId = CreateFolderOnGoogleDriver(Globals.CLOUD_EVATEL_FOLDER, service);
            }

            // Loop files to upload all files
            foreach (FileInfo file in files)
            {
                var FileMetaData = new Google.Apis.Drive.v3.Data.File()
                {
                    Name     = file.Name,
                    MimeType = MimeMapping.GetMimeMapping(file.FullName),
                    Parents  = new List <string>
                    {
                        folderId
                    }
                };


                FilesResource.CreateMediaUpload request;

                using (var stream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open))
                {
                    request        = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
                    request.Fields = "id";
                    request.Upload();
                }


                // Clear session
                //System.Web.HttpContext.Current.Session.Remove("uploadedFileName");

                // Check upload result
                var uploadedfile = request.ResponseBody;

                // Set file processing result
                FileProcessingResult result = new FileProcessingResult();
                result.UserId           = userId;
                result.FileName         = file.Name;
                result.CloudStorageType = (byte)User.ServiceType.GoogleDrive;
                result.TimeStamp        = DateTime.Now;

                // Upload successfully
                if (uploadedfile.Id != null)
                {
                    result.IsSuccessful = true;
                }
                // Upload failed
                else
                {
                    result.IsSuccessful = false;
                }

                // Add result to context
                _context.FilesProcessingResults.Add(result);
            }

            // Save to Database;
            _context.SaveChanges();

            service.Dispose();
        }
        public static void UploadFilesToFtpServer(int userId, FileInfo[] files)
        {
            // Get FtpClient
            FtpClient client = GetFtpClientByUserId(userId);



            try
            {
                // Connect to FTP Server
                client.Connect();

                // Check if the folder has been existing
                bool isFolderExists = client.DirectoryExists("/" + Globals.CLOUD_EVATEL_FOLDER + "/");

                // When folder is not existing, create a new folder
                if (!isFolderExists)
                {
                    client.CreateDirectory(Globals.CLOUD_EVATEL_FOLDER);
                }

                // Set working direcotry
                client.SetWorkingDirectory("/" + Globals.CLOUD_EVATEL_FOLDER + "/");
            }
            catch (Exception ex)
            {
                //ViewBag.ErrorMessage = Globals.FTP_CONNECTION_ERROR + "\n" + ex.Message;
                //return View("FtpServerForm");

                // Error handle will be done
                Debug.WriteLine("FTP connection error, " + ex.Message);
            }


            // Loop and upload all files to FTP Server
            foreach (FileInfo file in files)
            {
                try
                {
                    // upload a file
                    bool uploadedResult = client.UploadFile(file.FullName, file.Name);

                    // Set file processing result
                    FileProcessingResult result = new FileProcessingResult();
                    result.UserId           = userId;
                    result.FileName         = file.Name;
                    result.CloudStorageType = (byte)User.ServiceType.FtpServer;
                    result.TimeStamp        = DateTime.Now;

                    // Upload successfully
                    if (uploadedResult)
                    {
                        result.IsSuccessful = true;
                    }
                    // Upload failed
                    else
                    {
                        result.IsSuccessful = false;
                    }

                    // Add result to context
                    _context.FilesProcessingResults.Add(result);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Upload error, " + ex.Message);
                }
            }

            // Save to Database;
            _context.SaveChanges();

            client.Dispose();
        }