Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.activej.fs.http;
import io.activej.bytebuf.ByteBuf;
import io.activej.common.exception.MalformedDataException;
import io.activej.common.initializer.WithInitializer;
import io.activej.csp.ChannelConsumer;
import io.activej.csp.ChannelSupplier;
import io.activej.csp.dsl.ChannelConsumerTransformer;
import io.activej.csp.queue.ChannelZeroBuffer;
import io.activej.fs.ActiveFs;
import io.activej.fs.FileMetadata;
import io.activej.fs.exception.FsException;
import io.activej.http.*;
import io.activej.promise.Promise;
import io.activej.promise.SettablePromise;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Set;
import static io.activej.common.Checks.checkArgument;
import static io.activej.common.Utils.isBijection;
import static io.activej.csp.dsl.ChannelConsumerTransformer.identity;
import static io.activej.fs.http.FsCommand.*;
import static io.activej.fs.util.MessageTypes.STRING_META_MAP_TYPE;
import static io.activej.fs.util.RemoteFsUtils.*;
import static io.activej.http.HttpHeaders.CONTENT_LENGTH;
/**
* A client to the remote server with {@link ActiveFsServlet}.
* This client can be used to connect to publicly available servers.
*
* Inherits all the limitations of {@link ActiveFs} implementation located on server.
*/
public final class HttpActiveFs implements ActiveFs, WithInitializer {
private final IAsyncHttpClient client;
private final String url;
private HttpActiveFs(String url, IAsyncHttpClient client) {
this.url = url;
this.client = client;
}
public static HttpActiveFs create(String url, IAsyncHttpClient client) {
return new HttpActiveFs(url.endsWith("/") ? url : url + '/', client);
}
@Override
public Promise> upload(@NotNull String name) {
return doUpload(name, null);
}
@Override
public Promise> upload(@NotNull String name, long size) {
return doUpload(name, size);
}
@Override
public Promise> append(@NotNull String name, long offset) {
checkArgument(offset >= 0, "Offset cannot be less than 0");
UrlBuilder urlBuilder = UrlBuilder.relative()
.appendPathPart(APPEND)
.appendPath(name);
if (offset != 0) {
urlBuilder.appendQuery("offset", offset);
}
return uploadData(HttpRequest.post(url + urlBuilder.build()), identity());
}
@Override
public Promise> download(@NotNull String name, long offset, long limit) {
checkArgument(offset >= 0 && limit >= 0);
UrlBuilder urlBuilder = UrlBuilder.relative()
.appendPathPart(DOWNLOAD)
.appendPath(name);
if (offset != 0) {
urlBuilder.appendQuery("offset", offset);
}
if (limit != Long.MAX_VALUE) {
urlBuilder.appendQuery("limit", limit);
}
return client.request(
HttpRequest.get(
url + urlBuilder
.build()))
.then(HttpActiveFs::checkResponse)
.map(HttpMessage::takeBodyStream);
}
@Override
public Promise