Esempio n. 1
1
    public Task TestFileSave() {
      var response = new FileState {
        Name = "newBekti.png",
        Url = new Uri("https://www.parse.com/newBekti.png"),
        MimeType = "image/png"
      };
      var mockController = new Mock<IParseFileController>();
      mockController.Setup(obj => obj.SaveAsync(It.IsAny<FileState>(),
          It.IsAny<Stream>(),
          It.IsAny<string>(),
          It.IsAny<IProgress<ParseUploadProgressEventArgs>>(),
          It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
      var mockCurrentUserController = new Mock<IParseCurrentUserController>();
      ParseCorePlugins.Instance.FileController = mockController.Object;
      ParseCorePlugins.Instance.CurrentUserController = mockCurrentUserController.Object;

      ParseFile file = new ParseFile("bekti.jpeg", new MemoryStream(), "image/jpeg");
      Assert.AreEqual("bekti.jpeg", file.Name);
      Assert.AreEqual("image/jpeg", file.MimeType);
      Assert.True(file.IsDirty);

      return file.SaveAsync().ContinueWith(t => {
        Assert.False(t.IsFaulted);
        Assert.AreEqual("newBekti.png", file.Name);
        Assert.AreEqual("image/png", file.MimeType);
        Assert.AreEqual("https://www.parse.com/newBekti.png", file.Url.AbsoluteUri);
        Assert.False(file.IsDirty);
      });
    }
 /// <summary>
 /// Creates a new file from a stream and a name.
 /// </summary>
 /// <param name="name">The file's name, ideally with an extension. The file name
 /// must begin with an alphanumeric character, and consist of alphanumeric
 /// characters, periods, spaces, underscores, or dashes.</param>
 /// <param name="data">The file's data.</param>
 /// <param name="mimeType">To specify the content-type used when uploading the
 /// file, provide this parameter.</param>
 public ParseFile(string name, Stream data, string mimeType = null) {
   state = new FileState {
     Name = name,
     MimeType = mimeType
   };
   this.dataStream = data;
 }
    public void TestSecureUrl() {
      Uri unsecureUri = new Uri("http://files.parsetfss.com/yolo.txt");
      Uri secureUri = new Uri("https://files.parsetfss.com/yolo.txt");
      Uri randomUri = new Uri("http://random.server.local/file.foo");

      FileState state = new FileState {
        Name = "A",
        Url = unsecureUri,
        MimeType = null
      };

      Assert.AreEqual(unsecureUri, state.Url);
      Assert.AreEqual(secureUri, state.SecureUrl);

      // Make sure the proper port was given back.
      Assert.AreEqual(443, state.SecureUrl.Port);

      state = new FileState {
        Name = "B",
        Url = randomUri,
        MimeType = null
      };

      Assert.AreEqual(randomUri, state.Url);
      Assert.AreEqual(randomUri, state.Url);
    }
 internal ParseFile(string name, Uri uri, string mimeType = null) {
   state = new FileState {
     Name = name,
     Url = uri,
     MimeType = mimeType
   };
 }
    public Task TestFileControllerSaveWithEmptyResult() {
      var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, new Dictionary<string, object>());
      var mockRunner = CreateMockRunner(response);
      var state = new FileState {
        Name = "bekti.png",
        MimeType = "image/png"
      };

      var controller = new ParseFileController(mockRunner.Object);
      return controller.SaveAsync(state, dataStream: new MemoryStream(), sessionToken: null, progress: null).ContinueWith(t => {
        Assert.True(t.IsFaulted);
      });
    }
Esempio n. 6
0
        public Task <FileState> SaveAsync(FileState state,
                                          Stream dataStream,
                                          String sessionToken,
                                          IProgress <ParseUploadProgressEventArgs> progress,
                                          CancellationToken cancellationToken = default(CancellationToken))
        {
            if (state.Url != null)
            {
                // !isDirty
                return(Task <FileState> .FromResult(state));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                var tcs = new TaskCompletionSource <FileState>();
                tcs.TrySetCanceled();
                return(tcs.Task);
            }

            var oldPosition = dataStream.Position;
            var command     = new ParseCommand("/1/files/" + state.Name,
                                               method: "POST",
                                               sessionToken: sessionToken,
                                               contentType: state.MimeType,
                                               stream: dataStream);

            return(commandRunner.RunCommandAsync(command,
                                                 uploadProgress: progress,
                                                 cancellationToken: cancellationToken).OnSuccess(uploadTask => {
                var result = uploadTask.Result;
                var jsonData = result.Item2;
                cancellationToken.ThrowIfCancellationRequested();

                return new FileState {
                    Name = jsonData["name"] as string,
                    Url = new Uri(jsonData["url"] as string, UriKind.Absolute),
                    MimeType = state.MimeType
                };
            }).ContinueWith(t => {
                // Rewind the stream on failure or cancellation (if possible)
                if ((t.IsFaulted || t.IsCanceled) && dataStream.CanSeek)
                {
                    dataStream.Seek(oldPosition, SeekOrigin.Begin);
                }
                return t;
            }).Unwrap());
        }
    public Task<FileState> SaveAsync(FileState state,
        Stream dataStream,
        String sessionToken,
        IProgress<ParseUploadProgressEventArgs> progress,
        CancellationToken cancellationToken = default(CancellationToken)) {
      if (state.Url != null) {
        // !isDirty
        return Task<FileState>.FromResult(state);
      }

      if (cancellationToken.IsCancellationRequested) {
        var tcs = new TaskCompletionSource<FileState>();
        tcs.TrySetCanceled();
        return tcs.Task;
      }

      var oldPosition = dataStream.Position;
      var command = new ParseCommand("files/" + state.Name,
          method: "POST",
          sessionToken: sessionToken,
          contentType: state.MimeType,
          stream: dataStream);

      return commandRunner.RunCommandAsync(command,
          uploadProgress: progress,
          cancellationToken: cancellationToken).OnSuccess(uploadTask => {
            var result = uploadTask.Result;
            var jsonData = result.Item2;
            cancellationToken.ThrowIfCancellationRequested();

            return new FileState {
              Name = jsonData["name"] as string,
              Url = new Uri(jsonData["url"] as string, UriKind.Absolute),
              MimeType = state.MimeType
            };
          }).ContinueWith(t => {
            // Rewind the stream on failure or cancellation (if possible)
            if ((t.IsFaulted || t.IsCanceled) && dataStream.CanSeek) {
              dataStream.Seek(oldPosition, SeekOrigin.Begin);
            }
            return t;
          }).Unwrap();
    }
        public Task TestFileControllerSave()
        {
            var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted,
              new Dictionary<string, object>() {
            { "name", "newBekti.png"  },
            { "url", "https://www.parse.com/newBekti.png" }
              });
              var mockRunner = CreateMockRunner(response);
              var state = new FileState {
            Name = "bekti.png",
            MimeType = "image/png"
              };

              var controller = new ParseFileController(mockRunner.Object);
              return controller.SaveAsync(state, dataStream: new MemoryStream(), sessionToken: null, progress: null).ContinueWith(t => {
            Assert.False(t.IsFaulted);
            var newState = t.Result;

            Assert.AreEqual(state.MimeType, newState.MimeType);
            Assert.AreEqual("newBekti.png", newState.Name);
            Assert.AreEqual("https://www.parse.com/newBekti.png", newState.Url.AbsoluteUri);
              });
        }
 /// <summary>
 /// Saves the file to the Parse cloud.
 /// </summary>
 /// <param name="progress">The progress callback.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 public Task SaveAsync(IProgress<ParseUploadProgressEventArgs> progress,
     CancellationToken cancellationToken) {
   return taskQueue.Enqueue(
       toAwait => FileController.SaveAsync(state, dataStream, ParseUser.CurrentSessionToken, progress, cancellationToken), cancellationToken)
   .OnSuccess(t => {
     state = t.Result;
   });
 }