Ejemplo n.º 1
0
        private SimplifiedServerResponse GenerateDeleteResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

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

            return(response);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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);
        }