Esempio n. 1
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 }).ToList().Clear();
                                _moduleCatalog.AddModule(module);
                            }

                            module.Ref = fileData.LocalFileName;
                            retVal     = module.ToWebModel();
                        }
                    }
                }

            return(Ok(retVal));
        }
 public static ModuleDescriptor ToWebModel(this coreModel.ManifestModuleInfo moduleInfo)
 {
     var retVal = new ModuleDescriptor();
     retVal.InjectFrom(moduleInfo);
     retVal.Version = moduleInfo.Version.ToString();
     retVal.PlatformVersion = moduleInfo.PlatformVersion.ToString();
     retVal.Groups = moduleInfo.Groups;
     if(moduleInfo.Dependencies != null)
     {
         retVal.Dependencies = moduleInfo.Dependencies.Select(x => new ModuleIdentity { Id = x.Id, Version = x.Version.ToString() }).ToList();
     }
     retVal.ValidationErrors = moduleInfo.Errors;
     return retVal;
 }