Merge pull request #470 from ONLYOFFICE/feature/routing-fix

Feature/routing fix
This commit is contained in:
Sergey Linnik
2023-11-15 10:16:47 +03:00
committed by GitHub
8 changed files with 51 additions and 47 deletions

View File

@ -48,6 +48,7 @@ import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -72,6 +73,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@ -149,6 +151,28 @@ public class FileController {
.body(resource);
}
private ResponseEntity<Resource> downloadSample(final String fileName) {
String serverPath = System.getProperty("user.dir");
String contentType = "application/octet-stream";
String[] fileLocation = new String[] {serverPath, "src", "main", "resources", "assets", "document-templates",
"sample", fileName};
Path filePath = Paths.get(String.join(File.separator, fileLocation));
Resource resource;
try {
resource = new UrlResource(filePath.toUri());
if (resource.exists()) {
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
// download data from the specified history file
private ResponseEntity<Resource> downloadFileHistory(final String fileName,
final String version,
@ -374,14 +398,12 @@ public class FileController {
@GetMapping("/assets")
public ResponseEntity<Resource> assets(@RequestParam("name")
final String name) { // get sample files from the assests
String fileName = Path.of("assets", "document-templates", "sample", fileUtility.getFileName(name)).toString();
return downloadFile(fileName);
return downloadSample(name);
}
@GetMapping("/csv")
public ResponseEntity<Resource> csv() { // download a csv file
String fileName = Path.of("assets", "document-templates", "sample", "csv.csv").toString();
return downloadFile(fileName);
return downloadSample("csv.csv");
}
@GetMapping("/files")

View File

@ -74,24 +74,12 @@ function routers()
$view->render();
return;
}
if (str_starts_with($path, '/assets')) {
$response = assets();
$response['status'] = 'success';
echo json_encode($response);
return;
}
if (str_starts_with($path, '/convert')) {
$response = convert();
$response['status'] = 'success';
echo json_encode($response);
return;
}
if (str_starts_with($path, '/csv')) {
$response = csv();
$response['status'] = 'success';
echo json_encode($response);
return;
}
if (str_starts_with($path, '/delete')) {
$response = delete();
$response['status'] = isset($response['error']) ? 'error' : 'success';

View File

@ -333,34 +333,6 @@ function files()
}
}
/**
* Download assets
*
* @return void
*/
function assets()
{
$fileName = basename($_GET["name"]);
$filePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "document-templates"
. DIRECTORY_SEPARATOR . "sample" . DIRECTORY_SEPARATOR . $fileName;
downloadFile($filePath);
}
/**
* Download a csv file
*
* @return void
*/
function csv()
{
$fileName = "csv.csv";
$filePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "document-templates"
. DIRECTORY_SEPARATOR . "sample" . DIRECTORY_SEPARATOR . $fileName;
downloadFile($filePath);
}
/**
* Download a file from history
*

View File

@ -212,11 +212,11 @@ final class DocEditorView extends View
// recipients data for mail merging
$dataSpreadsheet = $isEnableDirectUrl ? [
"fileType" => "csv",
"url" => serverPath(true) . "/csv",
"directUrl" => serverPath(false) . "/csv",
"url" => serverPath(true) . "/assets/document-templates/sample/csv.csv",
"directUrl" => serverPath(false) . "/assets/document-templates/sample/csv.csv",
] : [
"fileType" => "csv",
"url" => serverPath(true) . "/csv",
"url" => serverPath(true) . "/assets/document-templates/sample/csv.csv",
];
// users data for mentions

View File

@ -68,6 +68,7 @@ def routers():
path('convert', actions.convert),
path('create', actions.createNew),
path('csv', actions.csv),
path('assets', actions.assets),
path('download', actions.download),
path('downloadhistory', actions.downloadhistory),
path('edit', actions.edit),

View File

@ -458,6 +458,14 @@ def csv():
return response
# download a sample file
def assets(request):
filename = fileUtils.getFileName(request.GET['filename'])
filePath = os.path.join('assets', 'document-templates', 'sample', filename)
response = docManager.download(filePath)
return response
# download a file
def download(request):
try:

View File

@ -278,6 +278,18 @@ class HomeController < ApplicationController
send_file csvPath, :x_sendfile => true
end
# downloading an assets file
def assets
file_name = File.basename(params[:fileName])
asset_path = Rails.root.join('assets', 'document-templates', 'sample', file_name)
response.headers['Content-Length'] = File.size(asset_path).to_s
response.headers['Content-Type'] = MimeMagic.by_path(asset_path).type
response.headers['Content-Disposition'] = "attachment;filename*=UTF-8\'\'" + ERB::Util.url_encode(file_name)
send_file asset_path, :x_sendfile => true
end
# downloading a file
def download
begin

View File

@ -28,6 +28,7 @@ class Application < Rails::Application
root to: 'home#index'
match '/convert', to: 'home#convert', via: 'post'
match '/csv', to: 'home#csv', via: 'get'
match '/asset', to: 'home#assets', via: 'get'
match '/download', to: 'home#download', via: 'get'
match '/downloadhistory', to: 'home#downloadhistory', via: 'get'
match '/editor', to: 'home#editor', via: 'get'