Example #1
0
        private void HandleFatalWebException(WebException e, FtpDownloadRequest request)
        {
            switch ((e.Response as FtpWebResponse).StatusCode)
            {
            case FtpStatusCode.ActionNotTakenFileUnavailable:
                throw new InvalidFtpFileAccessException(request.FileName);

            default:
                throw new UnknownException(e);
            }
        }
Example #2
0
        public IActionResult Get(string id, string type)
        {
            FtpDownloadRequest request = new FtpDownloadRequest()
            {
                FileName = id,
                Folder   = ImageTypeFolderResolver[type == null ? "" : type],
                FtpInfo  = AppConfig.GetFtpConnectionInformation()
            };

            FtpDownloadResponse <byte[]> response = null;

            try
            {
                response = FtpDownloaderLogic.DownloadImage(request);
                return(File(response.FileContent, "image/jpeg"));
            }
            catch (Exception e)
            {
                string errorText = CreateErrorTextForFileQueryException(e);
                return(new JsonResult(new { error = errorText, fileName = id }));
            }
        }
Example #3
0
 public FtpDownloadResponse <string> DownloadText(FtpDownloadRequest request)
 {
     throw new NotImplementedException();
 }
Example #4
0
        public FtpDownloadResponse <byte[]> DownloadImage(FtpDownloadRequest request)
        {
            if (request.FtpInfo == null)
            {
                throw new InvalidFtpConnectionInformationException(request.FtpInfo);
            }

            string uri = $"ftp://{request.FtpInfo.Host}:{request.FtpInfo.Port}/";

            if (string.IsNullOrWhiteSpace(request.Folder))
            {
                uri += request.FileName;
            }
            else
            {
                uri += $"{request.Folder}/{request.FileName}";
            }

            FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(uri);

            ftpWebRequest.Credentials = new NetworkCredential(request.FtpInfo.UserName, request.FtpInfo.Password);
            ftpWebRequest.KeepAlive   = false;
            ftpWebRequest.UseBinary   = true;
            ftpWebRequest.EnableSsl   = request.FtpInfo.EnableSsl;
            ftpWebRequest.Method      = WebRequestMethods.Ftp.DownloadFile;

            byte[] fileContent = null;
            try
            {
                using (Stream stream = GetStream(ftpWebRequest, request.FileName))
                {
                    List <byte> fileContentList = new List <byte>();

                    byte[] buffer = new byte[102400];
                    int    bytes  = 0;
                    do
                    {
                        bytes = stream.Read(buffer, 0, buffer.Length);
                        if (bytes == buffer.Length)
                        {
                            fileContentList.AddRange(buffer);
                        }
                        else
                        {
                            byte[] tempBuffer = new byte[bytes];
                            for (int i = 0; i < bytes; i++)
                            {
                                tempBuffer[i] = buffer[i];
                            }
                            fileContentList.AddRange(tempBuffer);
                        }
                    } while (bytes != 0);

                    fileContent = fileContentList.ToArray();
                }
            }
            catch (Exception e)
            {
                if (e is WebException)
                {
                    HandleFatalWebException(e as WebException, request);
                }
            }

            return(new FtpDownloadResponse <byte[]>()
            {
                FileContent = fileContent,
                FileName = request.FileName
            });
        }