示例#1
0
        public void Remove(FileInfoEx fileInfoEx)
        {
            try
            {
                string directoryPath = ConfigurationSettings.AppSettings["FileTransferPath"];

                DirectoryInfo directoryInfo;
                if (!Directory.Exists(directoryPath))
                {
                    directoryInfo = Directory.CreateDirectory(directoryPath);
                }
                else
                {
                    directoryInfo = new DirectoryInfo(directoryPath);
                }

                string filePath = Path.Combine(directoryInfo.FullName, fileInfoEx.FileNameOnServer);

                File.Delete(filePath);

                using (FileTransferContext context = new FileTransferContext())
                {
                    FileInfoEx file =
                        context.Files.FirstOrDefault(f => f.FileNameOnServer.Equals(fileInfoEx.FileNameOnServer));

                    if (file != null)
                    {
                        context.Files.Remove(file);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#2
0
        private static void SaveFileInfoToDataBase(FileDataUpload file)
        {
            try
            {
                FileInfoEx fileInfoEx = new FileInfoEx
                {
                    FileNameOnServer = file.FileId,
                    OriginalFileName = file.FileName,
                    Author           = file.Author,
                    DateUpload       = file.DateUpload,
                    Description      = file.Description
                };

                using (FileTransferContext context = new FileTransferContext())
                {
                    context.Files.Add(fileInfoEx);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#3
0
        public FileDataDownload Download(FileInfoEx fileInfoEx)
        {
            FileDataDownload fileDataDownload = null;

            try
            {
                Stream stream;
                bool   fileIsFound = _filesDownloadStreams.TryGetValue(fileInfoEx.FileNameOnServer, out stream);

                if (fileIsFound)
                {
                    byte[] buffer    = new byte[BufferSize];
                    int    readBytes = stream.Read(buffer, 0, buffer.Length);

                    if (readBytes == 0)
                    {
                        stream.Close();
                        _filesDownloadStreams.TryRemove(fileInfoEx.FileNameOnServer, out stream);

                        fileDataDownload = new FileDataDownload
                        {
                            IsLastPart = true
                        };
                    }
                    else
                    {
                        byte[] data = new byte[readBytes];
                        Array.Copy(buffer, data, readBytes);

                        fileDataDownload = new FileDataDownload
                        {
                            Data       = data,
                            IsLastPart = false
                        };
                    }
                }
                else
                {
                    string directoryPath = ConfigurationSettings.AppSettings["FileTransferPath"];

                    DirectoryInfo directoryInfo;
                    if (!Directory.Exists(directoryPath))
                    {
                        directoryInfo = Directory.CreateDirectory(directoryPath);
                    }
                    else
                    {
                        directoryInfo = new DirectoryInfo(directoryPath);
                    }

                    string filePath = Path.Combine(directoryInfo.FullName, fileInfoEx.FileNameOnServer);

                    if (File.Exists(filePath))
                    {
                        FileStream fsSource = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                        _filesDownloadStreams.TryAdd(fileInfoEx.FileNameOnServer, fsSource);

                        byte[] buffer    = new byte[BufferSize];
                        int    readBytes = fsSource.Read(buffer, 0, buffer.Length);

                        if (readBytes == 0)
                        {
                            fsSource.Close();
                            _filesDownloadStreams.TryRemove(fileInfoEx.FileNameOnServer, out stream);

                            fileDataDownload = new FileDataDownload
                            {
                                IsLastPart = true
                            };
                        }
                        else
                        {
                            byte[] data = new byte[readBytes];
                            Array.Copy(buffer, data, readBytes);

                            fileDataDownload = new FileDataDownload
                            {
                                Data       = data,
                                IsLastPart = false
                            };
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(fileDataDownload);
        }