Esempio n. 1
0
        public PackEngine.PackageInfo WritePackage(Stream stream, out string storeFileName)
        {
            string tempStoreName = Path.Combine(TempDirName, Guid.NewGuid().ToString());

            using (Stream tempStream = WritePackage(tempStoreName))
                stream.CopyTo(tempStream);

            string tempStorePath  = Path.Combine(_storeRoot, tempStoreName);
            string finalStorePath = null;

            try
            {
                PackEngine.Engine        packEngine = new PackEngine.Engine();
                PackEngine.PackageReader reader     = packEngine.CreatePackageReader(tempStorePath);
                PackEngine.PackageInfo   info       = reader.GetInfo();
                if (info == null || !info.IsValid())
                {
                    throw new ApplicationException("Invalid package");
                }

                storeFileName  = MakeStoreFileName(info.Name, info.Version);
                finalStorePath = Path.Combine(_storeRoot, storeFileName);

                if (File.Exists(finalStorePath))
                {
                    File.Delete(finalStorePath);
                }

                (new FileInfo(finalStorePath)).Directory.Create();

                File.Move(tempStorePath, finalStorePath);

                return(info);
            }
            catch
            {
                if (finalStorePath != null && File.Exists(finalStorePath))
                {
                    File.Delete(finalStorePath);
                }

                storeFileName = null;

                return(null);
            }
            finally
            {
                if (File.Exists(tempStorePath))
                {
                    File.Delete(tempStorePath);
                }
            }
        }
Esempio n. 2
0
        public bool AddPackage(Stream stream)
        {
            lock (_guard)
            {
                string storeFileName = null;

                try
                {
                    PackEngine.PackageInfo info = _store.WritePackage(stream, out storeFileName);
                    if (info == null || !info.IsValid() || string.IsNullOrEmpty(storeFileName))
                    {
                        throw new ApplicationException("Failed to handle the package");
                    }

                    if (_index.GetEntry(info.Name, info.Version.ToString()) != null)
                    {
                        return(false);
                    }

                    _index.AddEntry(new PackageIndex.Entry {
                        Info = info, StoreFileName = storeFileName
                    });
                }
                catch
                {
                    if (!string.IsNullOrEmpty(storeFileName))
                    {
                        _store.DeletePackage(storeFileName);
                    }

                    throw;
                }
            }

            return(true);
        }