Beispiel #1
0
        /// <summary>
        /// Gets the image response message.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <returns></returns>
        protected virtual HttpResponseMessage GetImageResponseMessage(string filePath, int width = 0, int height = 0)
        {
            HttpResponseMessage result;
            var fileInfo        = new FileInfo(filePath);
            var resizedFilePath = ServerFileHelper.GetResizedImagePath(filePath, width, height);

            byte[] resizedImageByteArray;
            using (Image resizedImage = Image.FromFile(resizedFilePath))
            {
                resizedImageByteArray = ServerFileHelper.ConvertImageToByteArray(resizedImage, fileInfo.Extension);
            }

            if (resizedImageByteArray != null && resizedImageByteArray.Length > 0)
            {
                result = new HttpResponseMessage(HttpStatusCode.OK);
                MemoryStream dataStream = new MemoryStream(resizedImageByteArray);
                result.Content = new StreamContent(dataStream);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue(ServerFileHelper.ConvertImageExtToMediaTypeHeaderValue(fileInfo.Extension));
            }
            else // Failed to read image
            {
                result = new HttpResponseMessage(HttpStatusCode.NoContent);
            }
            return(result);
        }
Beispiel #2
0
        private SimplifiedServerResponse GenerateGetResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.Body = ServerFileHelper.GetFileContent(request.Url);
            }

            return(response);
        }
Beispiel #3
0
        private SimplifiedServerResponse GenerateDeleteResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 202;
                ServerFileHelper.DeleteFile(request.Url);
            }

            return(response);
        }
Beispiel #4
0
        /// <summary>
        /// Deletes the record.
        /// </summary>
        /// <param name="recordId">The record identifier.</param>
        protected override void DeleteRecord(int recordId)
        {
            var productImage = this.UnitOfWork.ProductImageDAO.SelectByID(recordId);

            if (productImage != null)
            {
                var oldFilePath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath(
                                                             AppConstants.ImageDirName), productImage.ImageFile);
                if (!ServerFileHelper.DeleteFile(oldFilePath))
                {
                    throw new BusinessException("Không thể xóa hình ảnh");
                }

                // Mark record has been deleted
                this.UnitOfWork.ProductImageDAO.Delete(recordId);
            }
        }
Beispiel #5
0
        private SimplifiedServerResponse GenerateHeadResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = new SimplifiedServerResponse()
            {
                HttpVersion = request.HttpVersion,
                Connection  = request.Connection,
                Cookies     = request.Cookies
            };

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 200;
                response.ContentType    = ServerFileHelper.GetContentType(request.Url);
                response.LastModified   = ServerFileHelper.GetModifyDate(request.Url);
            }
            else
            {
                response.ResponseStatus = 404;
            }

            return(response);
        }
Beispiel #6
0
        private SimplifiedServerResponse GeneratePutResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = new SimplifiedServerResponse()
            {
                HttpVersion     = request.HttpVersion,
                ContentLocation = request.Url,
                Connection      = request.Connection,
                Cookies         = request.Cookies
            };

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 204;
            }
            else
            {
                response.ResponseStatus = 201;
                ServerFileHelper.WriteFileContent(request.Url, request.MessageBody);
            }

            return(response);
        }
Beispiel #7
0
        public virtual HttpResponseMessage UploadFiles()
        {
            var funcName = "UploadFiles";

            Logger.DebugFormat("{0} <-- Start", funcName);
            HttpResponseMessage result = null;

            try
            {
                var statuses = new List <FileStatus>();
                var ownerId  = this.GetImageOwnerId(HttpContext.Current.Request.Form);

                if (!string.IsNullOrEmpty(ownerId))
                {
                    for (var i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                    {
                        var  file        = HttpContext.Current.Request.Files[i];
                        var  refFileName = string.Empty;
                        bool uploadResult;

                        if (ImageExtensions.Contains(Path.GetExtension(file.FileName).ToUpper())) // Uploaded file is image
                        {
                            // Decrease image width to less than 1024 before uploading
                            uploadResult = ServerFileHelper.UploadAndResizeFile(file,
                                                                                AppConstants.MaximumImageWidth, this.StorageRootPath, ref refFileName);
                        }
                        else // pdf or zip file
                        {
                            uploadResult = ServerFileHelper.UploadFile(file, this.StorageRootPath, ref refFileName);
                        }

                        if (uploadResult)                                    // Upload successful
                        {
                            this.PostProcessUploading(refFileName, ownerId); // Update image info to DB
                            statuses.Add(new FileStatus(refFileName, file.ContentLength));
                        }
                    }

                    // Save uploaded image info to DB
                    this.UnitOfWork.SaveChanges((exception) =>
                    {
                        // Deleted all uploaded files
                        for (var i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                        {
                            var file     = HttpContext.Current.Request.Files[i];
                            var filePath = Path.Combine(this.StorageRootPath, file.FileName);
                            ServerFileHelper.DeleteFile(filePath);
                        }
                        throw new DatabaseException((int)DatabaseErrorCode.UploadImage, exception);
                    });

                    // Write upload status
                    this.WriteJsonIframeSafe(HttpContext.Current, statuses);
                    result = new HttpResponseMessage(HttpStatusCode.OK);
                }
                else // Owner is empty
                {
                    result = new HttpResponseMessage(HttpStatusCode.NotImplemented);
                }
            }
            catch (Exception ex)
            {
                Logger.WarnFormat("{0} - Exception: {1}", funcName, ex);
                result = new HttpResponseMessage(HttpStatusCode.NotImplemented);
            }

            Logger.DebugFormat("{0} --> End", funcName);
            return(result);
        }