public void TestUploadSound_Null()
        {
            Initializer.Reset();
            StorageService storage = new StorageService();

            Assert.IsFalse(storage.UploadSound(_info1, _nullBuffer));
            Assert.IsFalse(storage.UploadSound(_info2, _nullBuffer));

            Assert.IsTrue(storage.UploadSound(_info1, _emptyBuffer));
            Assert.IsFalse(storage.UploadSound(_info1, _nullBuffer));
        }
        public HttpResponseMessage PostImage([FromUri] string id, [FromUri] string versionNumber, [FromBody] FileUploadRequest fileRequest)
        {
            using (IDatabaseService database = new DatabaseService())
            {
                User user = RequestContext.GetAuthenticatedUser();
                Device device = RequestContext.GetDevice();
                long indiagramId;
                long version;

                if (!long.TryParse(id, out indiagramId) || !long.TryParse(versionNumber, out version))
                {
                    return Request.CreateBadRequestResponse();
                }

                if (!database.HasIndiagramVersion(user.Id, version))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Version not found");
                }

                if (!database.CanPushInVersion(device.UserId, device.Id, version))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Can not update in this version, version is closed or didn't created by this device");
                }

                IndiagramInfo indiagramInfo = database.GetLastIndiagramInfo(user.Id, indiagramId);

                if (indiagramInfo == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Indiagram not found");
                }

                if (indiagramInfo.Version != version)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Can not modify old indiagram version");
                }

                string filename = fileRequest.Filename;
                byte[] buffer = fileRequest.Content;

                IStorageService storageService = new StorageService();
                if (!storageService.UploadImage(indiagramInfo, buffer))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.Conflict, "Indiagram image already exists and can't be replaced");
                }

                database.SetIndiagramImage(indiagramInfo, filename, buffer);
                return Request.CreateEmptyGoodReponse();
            }
        }
        public HttpResponseMessage Image([FromUri] string id)
        {
            long indiagramId;
            if (!long.TryParse(id, out indiagramId))
            {
                return Request.CreateBadRequestResponse();
            }

            Device device = RequestContext.GetDevice();
            using (IDatabaseService database = new DatabaseService())
            {
                IndiagramForDevice resultIndiagram = database.GetIndiagram(device, indiagramId);

                if (resultIndiagram == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Indiagram not found");
                }

                if (string.IsNullOrWhiteSpace(resultIndiagram.ImageHash))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No image file");
                }

                IStorageService storageService = new StorageService();
                byte[] content = storageService.DownloadImage(resultIndiagram.Id, resultIndiagram.Version);

                if (content == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error while downloading file");
                }

                return Request.CreateGoodReponse(new FileDownloadResponse
                {
                    FileName = resultIndiagram.ImageFile,
                    Content = content
                });
            }
        }