Exemple #1
0
        public async Task <IHttpActionResult> UploadFile()
        {
            try
            {
                var      request   = HttpContext.Current.Request;
                string[] SessionID = HttpContext.Current.Request.Headers.GetValues("X-userSession");

                string chunkRepo = this.chunkRepository + "\\" + SessionID[0];
                string fileRepo  = this.fileRepository + "\\" + SessionID[0];
                if (request.Files.Count == 0)
                {
                    return(Ok());
                }

                var validationRules = new Macmillan.CMS.Common.Models.FlowModels.FlowValidationRules();
                var status          = _flowJs.PostChunk(request, fileRepo, chunkRepo, validationRules);

                string errors = string.Empty;
                status.ErrorMessages.ForEach(x => errors += " " + x);

                if (!string.IsNullOrEmpty(errors))
                {
                    Logger.Error(errors);

                    return(StatusCode(HttpStatusCode.NotFound));
                }
                else if (status.Status == Macmillan.CMS.Common.Models.FlowModels.PostChunkStatus.Done)
                {
                    this.UploadFile(new FileInfo(Path.Combine(fileRepo, status.FileName)));
                }
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                return(StatusCode(HttpStatusCode.NotFound));
            }

            return(Ok());
        }
Exemple #2
0
        private Macmillan.CMS.Common.Models.FlowModels.FlowJsPostChunkResponse PostChunkBase(HttpRequest request, string folder, string chunkfolder, Macmillan.CMS.Common.Models.FlowModels.FlowValidationRules validationRules)
        {
            var chunk         = new Macmillan.CMS.Common.Models.FlowModels.FlowChunk();
            var requestIsSane = chunk.ParseForm(request.Form);

            if (!requestIsSane)
            {
                var errResponse = new Macmillan.CMS.Common.Models.FlowModels.FlowJsPostChunkResponse();
                errResponse.Status = Macmillan.CMS.Common.Models.FlowModels.PostChunkStatus.Error;
                errResponse.ErrorMessages.Add("damaged");
            }
            List <string> errorMessages = null;
            var           file          = request.Files[0];
            var           response      = new Macmillan.CMS.Common.Models.FlowModels.FlowJsPostChunkResponse {
                FileName = chunk.FileName, Size = chunk.TotalSize
            };
            var chunkIsValid = true;

            if (validationRules != null)
            {
                chunkIsValid = chunk.ValidateBusinessRules(validationRules, out errorMessages);
            }
            if (!chunkIsValid)
            {
                response.Status        = Macmillan.CMS.Common.Models.FlowModels.PostChunkStatus.Error;
                response.ErrorMessages = errorMessages;
                return(response);
            }

            var chunkFullPathName = GetChunkFilename(chunk.Number, chunk.Identifier, chunkfolder);

            try
            {
                Logger.Info("Entered more chunks upload");
                // create chunkfolder if it does not exist
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                if (!Directory.Exists(chunkfolder))
                {
                    Directory.CreateDirectory(chunkfolder);
                }
                // save file
                file.SaveAs(chunkFullPathName);
            }
            catch (Exception ex)
            {
                Logger.Error("Save Chunk Method", ex);
                throw;
            }

            // see if we have more chunks to upload. If so, return here
            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                Logger.Info("Entered more chunks upload");
                var chunkNameToTest = GetChunkFilename(i, chunk.Identifier, chunkfolder);
                var exists          = File.Exists(chunkNameToTest);
                if (!exists)
                {
                    response.Status = Macmillan.CMS.Common.Models.FlowModels.PostChunkStatus.PartlyDone;
                    return(response);
                }
            }

            // if we are here, all chunks are uploaded
            var fileAry = new List <string>();

            for (int i = 1, l = chunk.TotalChunks; i <= l; i++)
            {
                fileAry.Add("flow-" + chunk.Identifier + "." + i);
            }

            MultipleFilesToSingleFile(folder, fileAry, chunk.FileName, chunkfolder);
            for (int i = 0, l = fileAry.Count; i < l; i++)
            {
                try
                {
                    File.Delete(Path.Combine(chunkfolder, fileAry[i]));
                }
                catch (Exception ex)
                {
                    Logger.Error("File Delete ", ex);
                }
            }
            response.Status = Macmillan.CMS.Common.Models.FlowModels.PostChunkStatus.Done;
            return(response);
        }
Exemple #3
0
 public Macmillan.CMS.Common.Models.FlowModels.FlowJsPostChunkResponse PostChunk(HttpRequest request, string folder, string chunkfolder, Macmillan.CMS.Common.Models.FlowModels.FlowValidationRules validationRules)
 {
     return(PostChunkBase(request, folder, chunkfolder, validationRules));
 }