private void ImportImageFile(string productId, string filename) { var importPath = ImagesImportPath; if (string.IsNullOrEmpty(importPath)) { importPath = DiskStorage.GetStoreDataVirtualPath(_hccApp.CurrentStore.Id, "import/"); } var filePath = importPath + filename; var mappedPath = HostingEnvironment.MapPath(filePath); if (File.Exists(mappedPath)) { DiskStorage.CopyProductImage(_hccApp.CurrentStore.Id, productId, filePath, filename); } }
protected override object HandleAction(HttpContext context, HotcakesApplication hccApp) { var optUniqueId = context.Request.Params["optUniqueId"]; var productChoice = hccApp.CatalogServices.ProductOptions.Find(optUniqueId); if (productChoice == null || productChoice.OptionType != OptionTypes.FileUpload) { // not found context.Response.StatusCode = 404; context.Response.End(); return(null); } if (context.Request.Files.Count <= 0) { return(null); } try { var file = context.Request.Files[0]; var fileName = context.Request.Params["filename"]; var prePath = GetInitialPathOfUploadFolder(hccApp); var orderProductPath = string.Concat(prePath, "\\", optUniqueId); var subPath = string.Format(UPLOAD_PATH_FORMAT, orderProductPath, Path.GetFileName(Text.CleanFileName(fileName))); var fullFilePath = DiskStorage.GetStoreDataPhysicalPath(hccApp.CurrentStore.Id, subPath); //check if path directory exists var dirPath = Path.GetDirectoryName(fullFilePath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var downloadPath = VirtualPathUtility.ToAbsolute(DiskStorage.GetStoreDataVirtualPath(hccApp.CurrentStore.Id, subPath)); var extension = Path.GetExtension(fullFilePath); // // TODO: Update this DNN API call to check the User File list instead, after upgrading the minimum version to DNN 9.5/9.6 // if (Host.AllowedExtensionWhitelist.IsAllowedExtension(extension) == false) { return(new UploadResponse { StatusCode = 413, Message = "Invalid File Extension" }); } //Check if file is sent in chunks and chunk is present in request var totalChunks = context.Request["chunks"] ?? string.Empty; var chunks = -1; if (!int.TryParse(totalChunks, out chunks)) { chunks = -1; } var chunkNo = context.Request["chunk"] ?? string.Empty; var chunk = -1; if (!int.TryParse(chunkNo, out chunk)) { chunk = -1; } //If there are no chunks sent then the file is sent as one //Indicates Plain HTML4 Upload & 1 single file if (chunks == -1) { if (MaxUploadSize == 0 || context.Request.ContentLength <= MaxUploadSize) { if (OnUploadChunk(file.InputStream, 0, 1, fullFilePath) == false) { return(new UploadResponse { StatusCode = 500, Message = "Unable to write file" }); } } else { return(new UploadResponse { StatusCode = 413, Message = "Uploaded file is too large" }); } //Return the final virtual path of uploaded file return(new UploadResponse { StatusCode = 200, Message = downloadPath }); } //File is sent in chunk, so full size of file & chunk is unknown if (chunk == 0 && MaxUploadSize > 0 && context.Request.ContentLength * (chunks - 1) > MaxUploadSize) { return(new UploadResponse { StatusCode = 413, Message = "Uploaded file is too large" }); } //First Chunk if (chunk == 0) { // TODO: Delete old file on re-uploading of same file again with 0th chunk if (File.Exists(fullFilePath)) { File.SetAttributes(fullFilePath, FileAttributes.Normal); File.Delete(fullFilePath); } } //n'th chunk if (OnUploadChunk(file.InputStream, chunk, chunks, fullFilePath) == false) { return(new UploadResponse { StatusCode = 500, Message = "Unable to write file" }); } //last chunk if (chunk == chunks - 1) { //return the file's virtual download path return(new UploadResponse { StatusCode = 200, Message = downloadPath }); } //If no response is sent yet send the success response return(new UploadResponse { StatusCode = 200, Message = downloadPath }); } catch (Exception ex) { EventLog.LogEvent(ex); return(new UploadResponse { StatusCode = 500, Message = "Unable to write file" }); } }