public async Task<IHttpActionResult> UploadAssetToLocalFileSystem()
        {
            var retVal = new List<webModel.BlobInfo>();

            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }

            var uploadsPath = HostingEnvironment.MapPath(_uploadsUrl);
            var streamProvider = new CustomMultipartFormDataStreamProvider(uploadsPath);

            await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
            });

            foreach (var fileData in streamProvider.FileData)
            {
                var fileName = fileData.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

                var blobInfo = new webModel.BlobInfo
                {
                    Name = fileName,
                    Url = VirtualPathUtility.ToAbsolute(_uploadsUrl + fileName),
                    MimeType = MimeTypeResolver.ResolveContentType(fileName)
                };
                retVal.Add(blobInfo);
            }

            return Ok(retVal.ToArray());
        }
Esempio n. 2
0
        public async Task<IHttpActionResult> UploadModuleArchive()
        {
            EnsureModulesCatalogInitialized();

            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }
            webModel.ModuleDescriptor retVal = null;
            var uploadsPath = HostingEnvironment.MapPath(_uploadsUrl);
            var streamProvider = new CustomMultipartFormDataStreamProvider(uploadsPath);

            await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
            });

            var fileData = streamProvider.FileData.FirstOrDefault();

            using (var packageStream = File.Open(fileData.LocalFileName, FileMode.Open))
            using (var package = new ZipArchive(packageStream, ZipArchiveMode.Read))
            {
                var entry = package.GetEntry("module.manifest");
                if (entry != null)
                {
                    using (var manifestStream = entry.Open())
                    {
                        var manifest = ManifestReader.Read(manifestStream);
                        var module = new ManifestModuleInfo(manifest);
                        var alreadyExistModule = _moduleCatalog.Modules.OfType<ManifestModuleInfo>().FirstOrDefault(x => x.Equals(module));
                        if (alreadyExistModule != null)
                        {
                            module = alreadyExistModule;
                        }
                        else
                        {
                            //Force dependency validation for new module
                            _moduleCatalog.CompleteListWithDependencies(new[] { module }).ToArray();
                            _moduleCatalog.AddModule(module);
                         
                        }
                        module.Ref = fileData.LocalFileName;
                        retVal = module.ToWebModel();
                    }
                }
            }
            return Ok(retVal);
        }