예제 #1
0
        public static string GetStoragePath(object entity)
        {
            if (typeof(Photo).IsAssignableFrom(entity.GetType()))
            {
                Photo  p         = (Photo)entity;
                string extension = GetContentType(entity).Split('/')[1];

                return(Path.Combine(RootPhotosStoragePath, p.ID + "." + extension));
            }
            else if (entity.GetType() == typeof(NorthwindModel.Customers))
            {
                NorthwindModel.Customers c = (NorthwindModel.Customers)entity;
                return(Path.Combine(RootCustomersStoragePath, c.CustomerID + ".blob"));
            }
            else if (entity.GetType() == typeof(RowEntityTypeWithIDAsKey) && (((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerFullName || ((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerWithBirthdayFullName))
            {
                int id = ((RowEntityTypeWithIDAsKey)entity).ID;
                return(Path.Combine(RootCustomersStoragePath, id + ".blob"));
            }
            else if (entity.GetType() == typeof(RowComplexType) && (((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.Customer" || ((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.CustomerWithBirthday"))
            {
                int id = (int)((RowComplexType)entity).Properties["ID"];
                return(Path.Combine(RootCustomersStoragePath, id + ".blob"));
            }

            throw new NotSupportedException("Unsupported entity type: " + entity.GetType());
        }
예제 #2
0
        public Stream GetWriteStream(object entity, string etag, bool?checkETagForEquality, DataServiceOperationContext operationContext)
        {
            this.ThrowIfDisposed();
            CallOrderLog += "-GetWriteStream";
            bool returnEmptyStream;

            if (!operationContext.IsBatchRequest || !operationContext.AbsoluteServiceUri.MakeRelativeUri(operationContext.AbsoluteRequestUri).OriginalString.StartsWith("$"))
            {
                CheckETag(entity, etag, checkETagForEquality, operationContext, out returnEmptyStream);
            }

            string slug = operationContext.RequestHeaders["Slug"];

            if (entity.GetType() == typeof(Photo))
            {
                Photo p = (Photo)entity;

                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(slug))
                {
                    int id;
                    if (int.TryParse(slug, out id))
                    {
                        p.ID = id;
                    }
                    else
                    {
                        p.Description = slug;
                    }
                }
            }
            else if (entity.GetType() == typeof(NorthwindModel.Customers))
            {
                NorthwindModel.Customers c = (NorthwindModel.Customers)entity;

                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    Random rand = TestUtil.Random;
                    c.CustomerID  = "C" + rand.Next(1000, 9999).ToString();
                    c.CompanyName = "Microsoft";

                    if (!string.IsNullOrEmpty(slug))
                    {
                        c.CustomerID = slug;
                    }
                }
            }
            else if (entity.GetType() == typeof(NorthwindModel.Orders))
            {
                NorthwindModel.Orders o = (NorthwindModel.Orders)entity;
                if (operationContext.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    Random rand = TestUtil.Random;
                    o.OrderID = rand.Next(1000, 9999);
                }
            }
            else if (entity.GetType() == typeof(RowEntityTypeWithIDAsKey) && (((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerFullName || ((RowEntityTypeWithIDAsKey)entity).TypeName == CustomRowBasedContext.CustomerWithBirthdayFullName))
            {
                int id;
                if (int.TryParse(slug, out id))
                {
                    ((RowEntityTypeWithIDAsKey)entity).ID = id;
                }
                else
                {
                    ((RowEntityTypeWithIDAsKey)entity).ID = TestUtil.Random.Next(1000, 9999);
                }
            }
            else if (entity.GetType() == typeof(RowComplexType) && (((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.Customer" || ((RowComplexType)entity).TypeName == "AstoriaUnitTests.Stubs.CustomerWithBirthday"))
            {
                int id;
                if (int.TryParse(slug, out id))
                {
                    ((RowComplexType)entity).Properties["ID"] = id;
                }
                else
                {
                    ((RowComplexType)entity).Properties["ID"] = TestUtil.Random.Next(1000, 9999);
                }
            }

            ValidateArguments(entity, operationContext);
            SetCustomResponseHeaders(operationContext);

            Stream s;

            switch (DataServiceStreamProvider.ProviderStorageMode)
            {
            case StorageMode.Disk:
                s = File.Open(GetStoragePath(entity), FileMode.Create, FileAccess.Write);
                break;

            case StorageMode.LargeStream:
                s = new LargeStream();
                LargeStreamStorage[GetStoragePath(entity)] = (LargeStream)s;
                break;

            default:
                throw new InvalidOperationException("Unknown StorageMode!");
            }

            GetWriteStreamCalled = true;
            return(s);
        }